WPF—DispatcherTimer定时器

WPF---DispatcherTimer定时器

WPF界面是没有timer控件的,Winform有。但是我们可以使用DispatcherTimer来实现定时器。

在WPF应用程序中,DispatcherTimer是一种常用的计时器工具,它可以在指定的时间间隔触发事件。以下是一个简单的使用DispatcherTimer的例子:

cs 复制代码
public partial class MainWindow : Window
{
    private DispatcherTimer timer;
    private int count;
 
    public MainWindow()
    {
        InitializeComponent();
 
        // 创建计时器实例
        timer = new DispatcherTimer();
        // 设置间隔时间(例如,每隔1秒触发一次)
        timer.Interval = TimeSpan.FromSeconds(1);
        // 添加Tick事件处理程序
        timer.Tick += Timer_Tick;
        // 启动计时器
        timer.Start();
    }
 
    private void Timer_Tick(object sender, EventArgs e)
    {
        // 这里处理每次计时器触发的事件
        count++;
        // 假设有一个名为"txtCounter"的文本框来显示计数
        txtCounter.Text = count.ToString();
    }
}
相关推荐
qq_340474028 小时前
5.02 WPF的 Combox、ListBox,slider、ProgressBar使用
wpf
fkdw9 小时前
.net farmework 4.8 类库中添加 wpf 窗体
.net·wpf
八股文领域大手子10 小时前
Spring多数据源环境下的事务与数据源切换
java·数据库·后端·mysql·spring·wpf
勘察加熊人10 小时前
c#使用wpf实现helloworld和login登录
开发语言·c#·wpf
她说彩礼65万11 小时前
WPF 自定义路由事件
wpf
qq_3404740211 小时前
5.2.1 WPF 通过ItemControl自己做柱状图
java·开发语言·wpf
蓝点lilac12 小时前
C# 窗口过程消息处理 WndProc
wpf·winform·winapi
她说彩礼65万1 天前
WPF 依赖项属性
wpf
观无1 天前
WPF的MVVM的基础知识
wpf
她说彩礼65万2 天前
WPF 自定义行为AssociatedObject详解
wpf