WPF 实现倒计时

效果:

UI:放置一个Label ---><Label Name="lblSecond" FontSize="20" Foreground="Red" ></Label>

CS:

  private int countSecond=300; //记录秒数

  private void UserControl_Loaded(object sender, RoutedEventArgs e)

  {

    private DispatcherTimer disTimer = new DispatcherTimer();

    disTimer.Interval = new TimeSpan(0, 0, 0, 1); //参数分别为:天,小时,分,秒。此方法有重载,可根据实际情况调用。

    disTimer.Tick += new EventHandler(disTimer_Tick); //每一秒执行的方法

    disTimer.Start();

  }

  void disTimer_Tick(object sender, EventArgs e)

  {

    if(countSecond==0)

    {

      MessageBox.Show("结束");

    }

    else

    {

      //判断lblSecond是否处于UI线程上

      if (lblSecond.Dispatcher.CheckAccess())

      {

        lblSecond.Content=countSecnd.ToString();

      }

      else

      {

        lblSecond.Dispatcher.BeginInvoke(DispatcherPriority.Normal,(Action)(() =>{

          lblSecond.Content=countSecond.ToString();

        }));  

      }

      countSecond--;

    }

  }

  

 

  

转载于:https://www.cnblogs.com/chasecnblogs/archive/2012/12/27/2835321.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值