一个简单的 WPF 程序,用于显示实时时间
2024-08-21 01:23:56
直接贴代码了:
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");
}
}
}
运行截图
谢谢浏览!
最新文章
- jquery隐藏侧边栏和折叠侧边栏方法
- git 使用 总结
- 配置MAVEN出现错误:java_home not found in your enviroment
- a链接的四种状态
- Eclipse tomcat先启动成功,然后再报超时原因之一
- Linux Direct 文件读写(文件DIO)
- listen和accept函数
- 【转】NSDictionary以及NSMutableDictionary的用法
- android浪漫樱花凋零动态壁纸应用源码
- 利用http协议实现图片窃取
- [转] React Router 使用教程
- api (三)文本字符输出 (转)
- location的用法
- 超赞网页背景效果-canvas-nest.js
- <;WebGIS之OpenLayers全面解析>;示例程序运行问题
- Idea书签管理器的使用
- 搭建Java后台
- Django2.0的path方法无法使用正则表达式的解决办法
- ros rviz 启动指定的rviz 文件
- Checkpoint--实现步骤