WPF C# - 定时器倒计时 [英] WPF C# - Timer countdown

查看:565
本文介绍了WPF C# - 定时器倒计时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何能实现我的一段代码写在WPF C#以下?

How can I implement the following in my piece of code written in WPF C#?

我有我已经实现了一个SelectionChanged事件而(由ElementFlow控制。定义)触发了在控件的项目选择已经改变了特定的事件

I have a ElementFlow control in which I have implemented a SelectionChanged event which (by definition) fires up a specific event when the control's item selection has changed.

我想什么它做的是:


  1. 启动例如一个计时器

  2. 如果定时器达到2秒钟,然后推出一个消息说(你好)

  3. 如果定时器之前选择的变化达到2秒钟,然后定时器应清零,重新开始了。

这是保证,如果选择没有在2秒内改变,但我不熟悉WPF的DispatcherTimer功能,因为我更了解内情,当涉及到Windows窗体的正常定时器冗长的操作仅启动。

This is to ensure that the lengthy action only launches if the selection has not changed within 2 seconds but I am not familiar with the DispatcherTimer feature of WPF as i am more in the know when it comes to the normal Timer of Windows Forms.

谢谢,

秒。

推荐答案

感谢您的帮助,我已经想通了完整代码了这样:

Thanks to your help, I've figured the complete code out as such:

DispatcherTimer _timer;

public MainWindow()
{
    _myTimer = new DispatcherTimer();
    _myTimer.Tick += MyTimerTick;
    _myTimer.Interval = new TimeSpan(0,0,0,1);
}

private void ElementFlowSelectionChanged(object sender, SelectionChangedEventArgs e)
{
    _counter = 0;
    _myTimer.Stop();
    _myTimer.Interval = new TimeSpan(0, 0, 0, 1);
    _myTimer.Start();
}

private int _counter;
public int Counter
{
    get { return _counter; }
    set
        {
            _counter = value;
            OnPropertyChanged("Counter");
        }
}
private void MyTimerTick(object sender, EventArgs e)
{
    Counter++;
    if (Counter == 2)
    {
        _myTimer.Stop();
        MessageBox.Show("Reached the 2 second countdown");
    }
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
    PropertyChangedEventHandler e = PropertyChanged;
    if (e != null)
        {
            e(this, new PropertyChangedEventArgs(propertyName));
}
        }



非常感谢你!

Thank you very much!!!

这篇关于WPF C# - 定时器倒计时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆