Xamarin.Android 使用Timer 并更改UI
http://blog.csdn.net/ozhangsan12345/article/details/72653070
第一步:创建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();
- }