我已经使用以下代码实现了递增计时器(或秒表)。我想知道是否存在使用c#在Windows Phone中实现此目标的有效且标准的方法。

任何建议,将不胜感激。

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 + ":";
}

最佳答案

为什么不使用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();
}

10-01 03:12