0

我是一个菜鸟,我有这个问题,我需要为我的程序制作一个计时器,当用户单击特定按钮时它就会启动。然后将倒计时输出到标签

提前致谢

4

2 回答 2

1

这是从生产中提取的,所以我确信它有效:

...
     _Timer = new DispatcherTimer();
     _Timer.Interval = TimeSpan.FromMilliseconds(125);
     _Timer.Tick += new EventHandler(_Timer_Tick);
     _Timer.IsEnabled = true;
     _Timer.Start();
...

    void _Timer_Tick(object sender, EventArgs e)
    {
         try {
           ...Do your thing here
         } catch (Exception x){
             Debug.WriteLine("Error: "+x);
         }
    }
于 2013-01-30T18:39:39.000 回答
1

位于命名空间DispatcherTimerSystem.Windows.Threading

像这样的东西应该工作:

public partial class MainWindow : Window
{
    int count = 0;
    System.Windows.Threading.DispatcherTimer tmr = new System.Windows.Threading.DispatcherTimer();
    public MainWindow()
    {
        InitializeComponent();
        tmr.Interval = new TimeSpan(0, 0, 2);
        tmr.Tick += new EventHandler(tmr_Tick);
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        tmr.Start();
    }

    void tmr_Tick(object sender, EventArgs e)
    {
        label1.Content = count += 1;
    }

    private void button2_Click(object sender, RoutedEventArgs e)
    {
        tmr.Stop();
    }
}
于 2013-01-30T18:41:08.090 回答