zoukankan      html  css  js  c++  java
  • Xamarin.Android 使用Timer 并更改UI

    http://blog.csdn.net/ozhangsan12345/article/details/72653070

    第一步:创建timer对象

    1. //创建timer对象  
    2.       Timer _dispatcherTimer;  
    3.       //计数  
    4.       int sec = 60;  


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

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

    //执行方法

    1. public void Tick(object state)  
    2.       {  
    3.           this.RunOnUiThread(() =>  
    4.           {  
    5.               if (sec > 0)  
    6.               {  
    7.                   smsbt.Text = sec.ToString() + "秒可重发";   
    8.                   sec--;  
    9.               }  
    10.               else  
    11.               {  
    12.                   _dispatcherTimer.Dispose();  
    13.                   sec = 60;  
    14.                   smsbt.Text = "获取验证码";   
    15.   
    16.               }  
    17.           });  
    18.            
    19.       }  

    //使用

      1. {  
      2.                        TimerCallback timerDelegate = new TimerCallback(Tick);  
      3.                        _dispatcherTimer = new Timer(timerDelegate, null, 0, 1000);  
      4.                        ProgressDialog progressDialog = ProgressDialog.Show(this, "", "请稍后...");  
      5.                        new Thread(new ThreadStart(() =>  
      6.                        {  
      7.   
      8.                            string url = this.GetString(Resource.String.url) + "/AppServices/userServices.aspx?action=regSms";  
      9.   
      10.                            using (var http = new HttpClient())  
      11.                            {  
      12.                                var content = new FormUrlEncodedContent(new Dictionary<string, string>() {  
      13.                        { "phone",userphone.Text }  
      14.                            });  
      15.                                var response = http.PostAsync(url, content);  
      16.                                string me = response.Result.Content.ReadAsStringAsync().Result;  
      17.                                progressDialog.Dismiss();  
      18.                                this.RunOnUiThread(() =>  
      19.                                {  
      20.                                HandleResult(me);  
      21.                                });  
      22.                            }  
      23.                        })).Start();  
      24.                    } 
  • 相关阅读:
    MAC subline批处理
    Git 所有常用命令
    【React入门】React父子组件传值demo
    【fullGC】内存监控工具
    【dockerFile配置jvm 启动参数】dockerFile 参数解释
    【总结】程序人生,每天进步一点点,以及曾经的梦想
    【记录】Field required a single bean, but 2 were found:
    【maven 】jar包冲突-记一次冲突解决
    PAT甲题题解-1052. Linked List Sorting (25)-排序
    PAT甲题题解-1053. Path of Equal Weight (30)-dfs
  • 原文地址:https://www.cnblogs.com/LuoEast/p/8013007.html
Copyright © 2011-2022 走看看