WPF计时器功能

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

本文实现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代码如下

 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的计时器功能如果您有更好的方法希望您也分享出来。

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6