1 /// <summary> 2 /// 倒計時類(發送驗證碼按鈕,點擊後,會倒計時60s,以後才能再次點擊。不一樣界面的多個驗證碼按鈕共享這個倒計時時間。)同一手機號碼1分鐘只能發1條; 3 /// </summary> 4 public static class CountDown 5 { 6 /// <summary> 7 /// 倒計時60秒 8 /// </summary> 9 public static int stTimeCount = 0; 10 11 /// <summary> 12 /// 倒計時60s方法 13 /// </summary> 14 /// <param name="btnCode"></param> 15 /// <param name="timeCount"></param> 16 public static void ShowCountDown(Button btnCode, int timeCount) 17 { 18 stTimeCount = timeCount; 19 DispatcherTimer dispatcherTimer = new DispatcherTimer(); 20 dispatcherTimer.Interval = new TimeSpan(0, 0, 1); 21 dispatcherTimer.Start(); 22 int count = stTimeCount; 23 int i = 0; 24 dispatcherTimer.Tick += delegate 25 { 26 if (count > 0) 27 count--; 28 //倒計時:設置按鈕的值,以及按鈕不可點擊 29 btnCode.Content = count + " S "; 30 btnCode.IsEnabled = false; 31 stTimeCount = count; 32 if (count == i) 33 { 34 //倒計時完成: 設置按鈕的值,以及按鈕可用 35 dispatcherTimer.Stop(); 36 btnCode.Content = "獲取驗證碼"; 37 btnCode.IsEnabled = true; 38 stTimeCount = count ; 39 } 40 }; 41 } 42 }
4.修改登陸密碼界面構造函數中進行調用安全
1 public SetLoginPwd() 2 { 3 this.InitializeComponent(); 4 5 //獲取倒計時60秒是否有值,有值則繼續倒計時 6 if (CountDown.stTimeCount > 0) 7 { 8 CountDown.ShowCountDown(btnCode, CountDown.stTimeCount); 9 } 10 }