zoukankan      html  css  js  c++  java
  • vs xamarin android 读取rest

    private void Btn_Click(object sender, EventArgs e)
    {
        var u = FindViewById<EditText>(Resource.Id.editText1).Text;
        var p = FindViewById<EditText>(Resource.Id.editText2).Text;
        var progressDialog = ProgressDialog.Show(this, "Please wait...", "Checking account info...", true);
        var t=new Thread(new ThreadStart(delegate
        {
            var r = Api.CheckUser(u, p);
            if (r.HasValue)
            {
                RunOnUiThread(() => progressDialog.Hide());
                AppConfig.Config.SetNowUserId(r.Value);
                StartActivity(typeof(MainActivity));
                Finish();
            }
            else
            {
                RunOnUiThread(() => Toast.MakeText(this, "用户名或密码错误", ToastLength.Long).Show());
                RunOnUiThread(() => progressDialog.Hide());
            }
                    
        }));
        t.Start();
    }

    把loading框显示出来,然后开新线程做事,之后关闭loading框。

    因为是开新线程,所以如果希望更新界面,需要使用 RunOnUiThread方法

    还有另外的方式

    private readonly TaskScheduler uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
    private void Btn_Click(object sender, EventArgs e)
    {
        var u = FindViewById<EditText>(Resource.Id.editText1).Text;
        var p = FindViewById<EditText>(Resource.Id.editText2).Text;
        var progressDialog = ProgressDialog.Show(this, "Please wait...", "Checking account info...", true);
        Task.Factory.StartNew(() =>
        {
            return Api.CheckUser(u, p);
        }).ContinueWith(r =>
        {
            progressDialog.Hide();
            if (r.Result.HasValue)
            {
                AppConfig.Config.SetNowUserId(r.Result.Value);
                StartActivity(typeof(MainActivity));
                Finish();
            }
            else
            {
                Toast.MakeText(this, "用户名或密码错误", ToastLength.Long).Show();
            }
                    
        }, uiScheduler);
    }

    使用Task的ContinueWith,可以指定在ui上执行

    访问网络需要给permission,在项目属性里,Android Manifest 下面列出了所有的permission,打钩internet

  • 相关阅读:
    Python列表去重的三种方法
    关于Python的 a, b = b, a+b
    Python爬取B站视频信息
    Linux文件管理命令
    (一)MySQL学习笔记
    Linux特殊字符含义
    在父容器div中图片下方有一条空隙问题
    对Json的各种遍历方法
    for循环使用append问题
    IE兼容性
  • 原文地址:https://www.cnblogs.com/czcz1024/p/4569764.html
Copyright © 2011-2022 走看看