Xamarin.Android 使用timer 并更改UI

来源:互联网 发布:网络推广的工作挣钱吗 编辑:程序博客网 时间:2024/05/01 14:49

第一步:创建timer对象


  //创建timer对象        Timer _dispatcherTimer;        //计数        int sec = 60;

第二步: 实例化timer并给委托事件


 TimerCallback timerDelegate = new TimerCallback(Tick); //tick为执行防范 _dispatcherTimer = new Timer(timerDelegate, null, 0, 1000);  

//执行方法

  public void Tick(object state)        {            this.RunOnUiThread(() =>            {                if (sec > 0)                {                    smsbt.Text = sec.ToString() + "秒可重发";                     sec--;                }                else                {                    _dispatcherTimer.Dispose();                    sec = 60;                    smsbt.Text = "获取验证码";                 }            });                   }


//使用


 {                        TimerCallback timerDelegate = new TimerCallback(Tick);                        _dispatcherTimer = new Timer(timerDelegate, null, 0, 1000);                        ProgressDialog progressDialog = ProgressDialog.Show(this, "", "请稍后...");                        new Thread(new ThreadStart(() =>                        {                            string url = this.GetString(Resource.String.url) + "/AppServices/userServices.aspx?action=regSms";                            using (var http = new HttpClient())                            {                                var content = new FormUrlEncodedContent(new Dictionary<string, string>() {                        { "phone",userphone.Text }                            });                                var response = http.PostAsync(url, content);                                string me = response.Result.Content.ReadAsStringAsync().Result;                                progressDialog.Dismiss();                                this.RunOnUiThread(() =>                                {                                HandleResult(me);                                });                            }                        })).Start();                    }


原创粉丝点击