public partial class MainWindow : Window
{
enum State
{
Start, Pause, End
}
//状态
State _state=State.End;
//计时
TimeSpan _timeSpan=new TimeSpan(0,0,0,0);
public MainWindow()
{
InitializeComponent();
var t = new DispatcherTimer();
t.Interval = new TimeSpan(0, 0, 0, 1);
t.Tick += OnTimer;
t.IsEnabled = true;
t.Start();
}
//时钟回调
void OnTimer(object sender,EventArgs e)
{
switch(_state)
{
case State.Start:
{
_timeSpan += new TimeSpan(0, 0, 0, 1);
}
break;
case State.Pause:
{
}
break;
case State.End:
{
_timeSpan = new TimeSpan();
}
break;
}
var time = string.Format("{0:D2}:{1:D2}:{2:D2}", _timeSpan.Hours, _timeSpan.Minutes, _timeSpan.Seconds);
Text111.Text = time;
}
//停止
private void Button_Click_1(object sender, RoutedEventArgs e)
{
_state = State.End;
}
//开始
private void Button_Click(object sender, RoutedEventArgs e)
{
_state = State.Start;
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
_state = State.Pause;
}
}