我不知道为什么这个暂停按钮在这里不起作用。我的秒表类也没有 Restart 方法,所以我想通过结合“reset and start”来编写它。还有什么想法吗?或者关于如何使这个暂停按钮起作用的任何想法?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Threading;
using System.Diagnostics;
namespace PhoneApp2
{
public partial class MainPage : PhoneApplicationPage
{
Stopwatch sw = new Stopwatch();
DispatcherTimer newTimer = new DispatcherTimer();
enum TimerState
{
Unknown,
Stopped,
Paused,
Running
}
private TimerState _currentState = TimerState.Unknown;
public MainPage()
{
InitializeComponent();
newTimer.Interval = TimeSpan.FromMilliseconds(1000 / 30);
newTimer.Tick += OnTimerTick;
}
void OnTimerTick(object sender, EventArgs args)
{
UpdateUI();
}
private void Button_Stop(object sender, RoutedEventArgs e)
{
Stop();
}
private void Button_Pause(object sender, RoutedEventArgs e)
{
Pause();
}
private void Button_Start(object sender, RoutedEventArgs e)
{
Start();
}
void UpdateUI()
{
textClock.Text = sw.ElapsedMilliseconds.ToString("0.00");
}
void Start()
{
sw.Reset();
sw.Start();
newTimer.Start();
UpdateUI();
}
void Stop()
{
_currentState = TimerState.Stopped;
sw.Stop();
newTimer.Stop();
UpdateUI();
}
void Pause()
{
_currentState = TimerState.Paused;
sw.Stop();
newTimer.Stop();
UpdateUI();
}
void Resume()
{
if (_currentState == TimerState.Stopped)
{
sw.Reset();
}
_currentState = TimerState.Running;
sw.Start();
newTimer.Start();
UpdateUI();
}
}
}
谢谢。PS:我的 .NET 版本是“版本 4.5.50709”Microsoft Visual Studio Express 2012 for Windows Phone。根据此链接,我们应该在 Stopwatch 类中有一个 Restart 方法,但我的却没有!