1 定时器
第一页
<Window x:Class="_6._13.MainWindow"
http://schemas.microsoft.com/winfx/2006/xaml/presentation"
http://schemas.microsoft.com/winfx/2006/xaml"
Title="定时器" Height="600" Width="825">
<Grid>
<Label Name="TimeLabel" Width=" 200" Height=" 80" Background="#0f0ffccc" Foreground="Black" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" FontSize ="16 " Content=" 2021-6-13 15:34:00" />
<Label Name="CountLabel" Width=" 200" Height=" 80" Background="#0f0ffccc" Foreground="Black" VerticalAlignment="Top" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" FontSize ="16 " Content=" 1" />
<Button Width=" 120" Height=" 60" HorizontalAlignment="Left" VerticalAlignment="Top" Content="开始" Click="Start_Click"/>
<Button Width=" 120" Height=" 60" HorizontalAlignment="Left" Content="暂停" Click="Stop_Click" Margin="0,171,0,330" />
<Button Width=" 120" Height=" 60" HorizontalAlignment="Left" Content="计数" Click="Count_Click" Margin="0,329,0,172" />
<Button Width=" 120" Height=" 60" HorizontalAlignment="Left" Content="重置" Click="Reset_Click" Margin="0,501,0,0" />
<TextBox Name="RecordTextBox" Width=" 300" Height=" 400" HorizontalAlignment="Right" />
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace _6._13
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
DispatcherTimer _timer;
int _cnt;
public MainWindow()
{
InitializeComponent();
TimeLabel.Content = DateTime.Now.ToString();
_timer = new DispatcherTimer();
_timer.Interval = new TimeSpan(0, 0, 0, 0, 50);
_timer.Tick += new EventHandler(Timer_Tick);
_cnt = 0;
}
private void Timer_Tick(object sender, EventArgs e)
{
_cnt++;
TimeLabel.Content = DateTime.Now.ToString();
CountLabel.Content = _cnt.ToString();
}
private void Start_Click(object sender, RoutedEventArgs e)
{
_timer.Start();
}
private void Stop_Click(object sender, RoutedEventArgs e)
{
_timer.Stop();
}
private void Count_Click(object sender, RoutedEventArgs e)
{
RecordTextBox.Text += string.Format("{0},{1}\n", _cnt, TimeLabel.Content);
}
private void Reset_Click(object sender, RoutedEventArgs e)
{
_timer.Stop();
_cnt = 0;
CountLabel.Content = _cnt.ToString();
RecordTextBox.Text = string.Format("");
}
}
}