直接贴代码了:
TimeShowerWindow.xaml
<Window x:Class="HelloWorld.TimeShowerWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:HelloWorld" mc:Ignorable="d" Title="WPF Timer" Height="350" Width="525"> <Window.Resources> <Style TargetType="{x:Type Button}"> <Setter Property="Padding" Value="5" /> <Setter Property="Margin" Value="5" /> </Style> </Window.Resources> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <StackPanel Orientation="Horizontal"> <Button x:Name="startDispatcher" Content="Start Dispatcher Timer" Click="StartDispatcher" /> <Button x:Name="startTimer" Content="Start Timer" Click="StartTimer" /> <Button x:Name="reset" Content="Reset" IsEnabled="False" Click="Reset" /> </StackPanel> <Label x:Name="clock" Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="25" FontWeight="Bold" /> </Grid> </Window>
TimeShowerWindow.xaml.cs
using System; using System.ComponentModel; using System.Threading; using System.Windows; using System.Windows.Threading; namespace HelloWorld { /// <summary> /// TimeShowerWindow.xaml 的交互逻辑 /// </summary> public partial class TimeShowerWindow : Window { private DispatcherTimer dispatcherTimer; //方式1(推荐WPF开发时使用) private Timer timer; //方式2:通用方式 public TimeShowerWindow() { InitializeComponent(); // 方式1 - 对象初始化,以及指定相应的委托程序 dispatcherTimer = new DispatcherTimer { Interval = new TimeSpan(0, 0, 1) }; dispatcherTimer.Tick += OnDispatcherTimer; // 方式2 - 对象初始化,以及指定相应的委托程序 timer = new Timer(OnTimer, null, Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan); } /// <summary> /// 方式1 的按钮点击事件处理 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void StartDispatcher(object sender, RoutedEventArgs e) { dispatcherTimer.Start(); ShowTime(); EnableStartButtons(false); } /// <summary> /// 方式2 的按钮点击事件处理 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void StartTimer(object sender, RoutedEventArgs e) { timer.Change(TimeSpan.Zero, new TimeSpan(0, 0, 1)); EnableStartButtons(false); } /// <summary> /// 重置按钮的点击事件处理 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Reset(object sender, RoutedEventArgs e) { timer.Change(Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan); dispatcherTimer.Stop(); EnableStartButtons(true); clock.Content = string.Empty; } /// <summary> /// 启用或禁用按钮 /// </summary> /// <param name="enabled"></param> private void EnableStartButtons(bool enabled) { startDispatcher.IsEnabled = enabled; startTimer.IsEnabled = enabled; reset.IsEnabled = !enabled; } private void OnDispatcherTimer(object sender, EventArgs e) { ShowTime(); } private void OnTimer(object state) { //子线程中更新 UI 线程,必须使用 Dispatcher.Invoke(() => ShowTime()); } /// <summary> /// 此方法运行在 UI 线程 /// </summary> private void ShowTime() { clock.Content = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); } } }
运行截图
谢谢浏览!