一个简单的 WPF 程序,用于显示实时时间
2019-08-01 18:25 音乐让我说 阅读(2789) 评论(0) 编辑 收藏 举报直接贴代码了:
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"); } } }
运行截图
谢谢浏览!
作者:音乐让我说(音乐让我说 - 博客园)
出处:http://music.cnblogs.com/
文章版权归本人所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步