【发布时间】:2015-11-18 23:03:49
【问题描述】:
我正在尝试为 Windows 10 通用应用程序制作秒表功能。使用 DispatcherTimer 我已经能够得到一个可以工作的秒表,因为秒数最多为 60,触发分钟加 1,然后重置为零。秒数将继续递增,但当它重置为零时会有 1 秒的延迟。基本上,当它达到 60 时,秒数会重置为 0,下一秒会保持在 0,然后在下一秒上升到 1。有什么想法可能导致这种情况吗?非常感谢!
public sealed partial class MainPage : Page
{
DispatcherTimer secondstimer = new DispatcherTimer();
int secondscount = 0;
int minutescount = 0;
int hourscount = 0;
public MainPage()
{
this.InitializeComponent();
secondstimer.Interval = new TimeSpan(0, 0, 1);
secondstimer.Tick += Secondstimer_Tick;
SecondsTextBlock.Text = "00";
MinutesTextBlock.Text = "00";
HoursTextBlock.Text = "00";
}
private void Secondstimer_Tick(object sender, object e)
{
SecondsTextBlock.Text = secondscount++.ToString();
if (secondscount == 61)
{
minutescount++;
secondscount = 0;
MinutesTextBlock.Text = minutescount.ToString();
SecondsTextBlock.Text = secondscount.ToString();
}
if (minutescount == 61)
{
hourscount++;
minutescount = 0;
MinutesTextBlock.Text = minutescount.ToString();
HoursTextBlock.Text = hourscount.ToString();
}
}
}
【问题讨论】:
-
我在 60 岁时拥有它,但它会提前一秒
标签: c# xaml timer uwp stopwatch