直接贴代码了:

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(, , ) };
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(, , ));
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");
}
}
}

运行截图

谢谢浏览!

最新文章

  1. jquery隐藏侧边栏和折叠侧边栏方法
  2. git 使用 总结
  3. 配置MAVEN出现错误:java_home not found in your enviroment
  4. a链接的四种状态
  5. Eclipse tomcat先启动成功,然后再报超时原因之一
  6. Linux Direct 文件读写(文件DIO)
  7. listen和accept函数
  8. 【转】NSDictionary以及NSMutableDictionary的用法
  9. android浪漫樱花凋零动态壁纸应用源码
  10. 利用http协议实现图片窃取
  11. [转] React Router 使用教程
  12. api (三)文本字符输出 (转)
  13. location的用法
  14. 超赞网页背景效果-canvas-nest.js
  15. &lt;WebGIS之OpenLayers全面解析&gt;示例程序运行问题
  16. Idea书签管理器的使用
  17. 搭建Java后台
  18. Django2.0的path方法无法使用正则表达式的解决办法
  19. ros rviz 启动指定的rviz 文件
  20. Checkpoint--实现步骤

热门文章

  1. 记一次Python与C#的AES加密对接
  2. 害死人不偿命的(3n+1)猜想-PTA
  3. Linux less grep
  4. solidity定长数组和动态数组
  5. IDA中查看某函数引用问题
  6. Java生鲜电商平台-SpringCloud微服务架构中核心要点和实现原理
  7. MVC模式和Spring MVC初识
  8. JVM 参数配置
  9. SpringBoot(六) SpringBoot整合Swagger2(自动化生成接口文档)
  10. django4-模板进阶