wpf点击按钮后禁用按钮60s,并显示xx秒后重试
后台代码:
public partial class Login : Window
{
private System.Threading.Timer CountDown = null;
public Login()
{
InitializeComponent();
CountDown = new Timer(CountDown_Tick, this, Timeout.Infinite, Timeout.Infinite);
}
private void SendVerCode_Click(object sender, RoutedEventArgs e)
{
CountDown.Change(0, 1000);
}
int CountDownNum = 60;
private void CountDown_Tick(object state)
{
//限制60秒内不能重复点击发送验证码
this.Dispatcher.BeginInvoke(new Action(() =>
{
SendVerCode.Content = $"{CountDownNum}秒后重试";
CountDownNum--;
if (CountDownNum < 0)
{
CountDown.Change(Timeout.Infinite, Timeout.Infinite);
CountDownNum = 60;
SendVerCode.Content = "获取验证码";
}
}));
}
}
效果: