WPF计时器倒数 [英] WPF timer countdown
问题描述
我想知道WPF中的计时器。
I am wondering about timers in WPF.
我基本上知道如何使一个简单的倒数计时器(标签)像下面的代码那样倒数:
what i basically know is how to make a simple count down timer (label) count down like this code:
private void buttonStartOne_Click(object sender, RoutedEventArgs e)
{
counterOne = new DispatcherTimer();
counterOne.Tick += new EventHandler(counterOne_Tick);
counterOne.Interval = new TimeSpan(0, 0, 1);
counterOneTime = 10;
counterOne.Start();
}
private void counterOne_Tick(object sender, EventArgs e)
{
// code goes here
if (counterOneTime > 0)
{
counterOneTime--;
labelCounterOne.Content = counterOneTime + "s";
}
else
counterOne.Stop();
}
在上面的示例代码中,倒计时仅为10秒。
我想要的是,我不知道该怎么做:HH:mm:ss而不是倒数。
您会用3个独立的计数器和标签(每个时间单位一个)来做到这一点吗?
还是解决这个问题的更好方法?
In this example code above, the countdown is just 10 seconds. What i want, and dont know is how i should make it as: HH:mm:ss and than make it count down. Would you do that with 3 separate counters and labels (one for each time unit)? Or what should be a better way to tackle this one?
推荐答案
您可以转换您的 Timespan
到具有 ToString(字符串格式)的字符串方法。将结果字符串设置在标签上
You could convert your Timespan
to a string with the ToString(string format) method. Set the resulting string on your label
这篇关于WPF计时器倒数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!