WPF C#-计时器倒计时

WPF C#-计时器倒计时,c#,wpf,timer,dispatchertimer,C#,Wpf,Timer,Dispatchertimer,如何在用WPF C#编写的代码中实现以下内容 我有一个ElementFlow控件,我在其中实现了一个SelectionChanged事件,该事件(根据定义)在控件的项选择发生更改时触发一个特定事件 我希望它做的是: 启动计时器 如果计时器达到2秒,则启动一个消息框,例如说“你好” 如果选择在计时器达到2秒之前更改,则应重置计时器并重新启动 这是为了确保仅当选择在2秒内未更改时,才会启动冗长的操作,但我不熟悉WPF的Dispatchermer功能,因为我更了解Windows窗体的正常计时器 谢谢

如何在用WPF C#编写的代码中实现以下内容

我有一个ElementFlow控件,我在其中实现了一个SelectionChanged事件,该事件(根据定义)在控件的项选择发生更改时触发一个特定事件

我希望它做的是:

  • 启动计时器
  • 如果计时器达到2秒,则启动一个消息框,例如说“你好”
  • 如果选择在计时器达到2秒之前更改,则应重置计时器并重新启动
  • 这是为了确保仅当选择在2秒内未更改时,才会启动冗长的操作,但我不熟悉WPF的Dispatchermer功能,因为我更了解Windows窗体的正常计时器

    谢谢

    试试这个:

    private int timerTickCount = 0;
    private bool hasSelectionChanged = false;
    private DispatcherTimer timer;
    
    在构造函数或相关方法中:

    timer = new DispatcherTimer();
    timer.Interval = new TimeSpan(0, 0, 1); // will 'tick' once every second
    timer.Tick += new EventHandler(Timer_Tick);
    timer.Start();
    
    然后是一个事件处理程序:

    private void Timer_Tick(object sender, EventArgs e)
    {
        DispatcherTimer timer = (DispatcherTimer)sender;
        if (++timerTickCount == 2)
        {
            if (hasSelectionChanged) timer.Stop();
            else MessageBox.Show("Hi there");
        }
    }
    
    最后,要实现这一点,您只需在选择已根据您的
    SelectionChanged
    事件更改时设置
    hasSelectionChanged
    变量。

    尝试以下操作:

    private int timerTickCount = 0;
    private bool hasSelectionChanged = false;
    private DispatcherTimer timer;
    
    在构造函数或相关方法中:

    timer = new DispatcherTimer();
    timer.Interval = new TimeSpan(0, 0, 1); // will 'tick' once every second
    timer.Tick += new EventHandler(Timer_Tick);
    timer.Start();
    
    然后是一个事件处理程序:

    private void Timer_Tick(object sender, EventArgs e)
    {
        DispatcherTimer timer = (DispatcherTimer)sender;
        if (++timerTickCount == 2)
        {
            if (hasSelectionChanged) timer.Stop();
            else MessageBox.Show("Hi there");
        }
    }
    

    最后,为了实现这一点,当选择根据
    SelectionChanged
    事件发生更改时,只需设置
    hasSelectionChanged
    变量。

    看这里是如何使用dispatheriter的代码,您可以在其中添加自己的逻辑。那要看你了

     private void ListBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            DispatcherTimer timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(2000);
            timer.Tick += timer_Tick;
            timer.Start();
    
        }
    
        void  timer_Tick(object sender, object e)
        {
           // show your message here..
        }
    

    下面是如何使用dispatheriter的代码,您可以在其中添加自己的逻辑。那要看你了

     private void ListBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            DispatcherTimer timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(2000);
            timer.Tick += timer_Tick;
            timer.Start();
    
        }
    
        void  timer_Tick(object sender, object e)
        {
           // show your message here..
        }
    

    要使用Dispatcher,请执行以下操作:

        private DispatcherTimer _timer;
        public void StartTimer()
        {
            if (_timer == null)
            {
                _timer = new DispatcherTimer();
                _timer.Tick += _timer_Tick;
            }
    
            _timer.Interval = TimeSpan.FromSeconds(2);
            _timer.Start();
        }
    
        void _timer_Tick(object sender, EventArgs e)
        {
            MessageBox.Show("Hi there");
            _timer.Stop();
        }
    
        void SelectionChangedEvent()
        {
            StartTimer();
        }
    

    要使用Dispatcher,请执行以下操作:

        private DispatcherTimer _timer;
        public void StartTimer()
        {
            if (_timer == null)
            {
                _timer = new DispatcherTimer();
                _timer.Tick += _timer_Tick;
            }
    
            _timer.Interval = TimeSpan.FromSeconds(2);
            _timer.Start();
        }
    
        void _timer_Tick(object sender, EventArgs e)
        {
            MessageBox.Show("Hi there");
            _timer.Stop();
        }
    
        void SelectionChangedEvent()
        {
            StartTimer();
        }
    

    我已经计算出了完整的代码,如下所示:

    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));
    }
    }
    

    我已经计算出了完整的代码,如下所示:

    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));
    }
    }