krr*_*hna 1 c# timer windows-phone-7 windows-phone windows-phone-8
我使用下面的代码实现了计时器(或秒表).我想知道是否有一种有效且标准的方法可以在Windows Phone中使用c#实现这一点.
任何建议,将不胜感激.
private void start_click()
{
lhours = 0; lmins = 0; lsecs = 0; lmsecs = 0;
myDispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 1000); // 1000 Milliseconds
myDispatcherTimer.Tick += new EventHandler(Each_Tick);
myDispatcherTimer.Start();
}
public void Each_Tick(object o, EventArgs sender)
{
lsecs = lsecs + 1;
if (lsecs > 59)
{
lsecs = 0;
lmins = lmins + 1;
if (lmins > 59)
{
lmins = 0;
lhours = lhours + 1;
if (lhours > 23)
{
lhours = 0;
ldays = ldays + 1;
}
}
}
lblTimerDisplay.Text = ldays + ":" + lhours + ":" + lmins + ":" + lsecs + ":";
}
Run Code Online (Sandbox Code Playgroud)
为什么不Stopwatch上课?
System.Diagnostics.Stopwatch _sw = new System.Diagnostics.Stopwatch();
private void start_click()
{
if (!_sw.IsRunning)
{
_sw.Start();
}
else
{
_sw.Stop();
_sw.Reset();
}
}
private void Each_Tick(object sender, EventArgs e)
{
lblTimerDisplay.Text = _sw.Elapsed.ToString();
}
Run Code Online (Sandbox Code Playgroud)