六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 299|回复: 0

WPF 实现倒计时

[复制链接]

升级  72%

10

主题

10

主题

10

主题

童生

Rank: 1

积分
36
 楼主| 发表于 2013-1-3 15:37:47 | 显示全部楼层 |阅读模式
<div id="cnblogs_post_body">效果:
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--;
    }
  }
  

  
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表