本文实现WPF的计时器功能是通过system.timers.timer这个组件实现的。现在网上相关的资料有很多,我只是在自己的工作中刚好遇到要实现这个功能,中间也走了很多的弯路,不停的参考网上现有的资源,终于实现了基本的定时功能。希望本文可以帮助到您,让您花更少的时间来完成这个功能。
system.timers.timer的相关内容可以查看这个网址Timer 类。
显示的页面没有过多的修饰,重要的是功能。基础界面如下:
代码如下:
TestWindow.xaml
<Window x:Class="UI_SubForms_Wpf.TestWindow"
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:UI_SubForms_Wpf"
mc:Ignorable="d"
Title="TestWindow" Height="450" Width="800">
<Grid>
<Button Name="BtnStart" Click="BtnStart_OnClick" Content="开始" HorizontalAlignment="Left" Margin="251,194,0,0" VerticalAlignment="Top"/>
<Button Name="BtnPause" Click="BtnPause_OnClick" Content="暂停" HorizontalAlignment="Left" Margin="390,194,0,0" VerticalAlignment="Top"/>
<Button Name="BtnStop" Click="BtnStop_OnClick" Content="停止" HorizontalAlignment="Left" Margin="320,194,0,0" VerticalAlignment="Top"/>
<TextBox Name="TbShowTime" HorizontalAlignment="Left" Margin="294,141,0,0" TextWrapping="Wrap" Text="00:00:00" VerticalAlignment="Top" Width="120"/>
</Grid>
</Window>
点击【开始】按钮,计时器开始计时,文本框显示计时数据,显示如下:
点击【暂停】按钮,计时器暂停计时,文本框显示计时数据,暂停按钮内容改为【继续】显示如下:
点击【继续】按钮,计时器继续计时,文本框显示计时数据,继续按钮内容改为【暂停】显示如下:
点击【停止】按钮,计时器停止计时,文本框显示计时数据,显示如下:
再次点击【开始】按钮,计时器开始重新开始计时,文本框显示计时数据,类似实现重置功能,显示如下:
相关的TestWindow.xaml.cs代码如下:文章来源:https://www.toymoban.com/news/detail-447427.html
public partial class TestWindow : Window
{
//计时器
System.Timers.Timer timer;
//委托
delegate void SetTextCallback(int TimeSpan);
public int seconds = 0;
public TestWindow()
{
InitializeComponent();
}
private void InitTimer()
{
//释放计时器占用资源,也就是“销毁计时器”
if (timer != null)
timer.Dispose();
//实例化一个计时器
timer = new System.Timers.Timer(1000);
timer.Start();
timer.Enabled = true;
seconds = 0;
timer.Elapsed += new ElapsedEventHandler(Timer_Elapsed);
}
private void Timer_Elapsed(object source, ElapsedEventArgs e)
{
timer.Stop();
//委托传值
this.Dispatcher.Invoke(new SetTextCallback(SetReallyTime), new object[] { seconds });
seconds++;
timer.Start();
}
private void SetReallyTime(int TimeSpan)
{
var timeSpan = new TimeSpan(0, 0, TimeSpan);
this.TbShowTime.Text = string.Format("{0:00}:{1:00}:{2:00}", (int)timeSpan.TotalHours, timeSpan.Minutes, timeSpan.Seconds);
}
//开始按钮
private void BtnStart_OnClick(object sender, RoutedEventArgs e)
{
InitTimer();
}
//暂停按钮
private void BtnPause_OnClick(object sender, RoutedEventArgs e)
{
if (BtnPause.Content == "暂停")
{
//计时器停止运行
timer.Enabled = false;
//改变按钮显示内容
this.BtnPause.Content = "继续";
}
else
{
//计时器继续运行
timer.Enabled = true;
//改变按钮显示内容
this.BtnPause.Content = "暂停";
}
}
//停止按钮
private void BtnStop_OnClick(object sender, RoutedEventArgs e)
{
//计时器停止运行
timer.Enabled = false;
seconds = 0;
}
}
以上就可以实现WPF的计时器功能,如果您有更好的方法,希望您也分享出来。文章来源地址https://www.toymoban.com/news/detail-447427.html
到了这里,关于WPF计时器功能的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!