【问题标题】:How do I create a timer for WPF如何为 WPF 创建计时器
【发布时间】:2018-08-28 13:48:06
【问题描述】:

我有一个button_click 事件和一个button_PreviewMouseLeftButtonDown 事件。我想为我的button_PreviewMouseLeftButtonDown 事件设置一个计时器。如果用户的鼠标按下时间超过 1 秒,那么我的代码将执行 button_PreviewMouseLeftButtonDown 事件。我怎样才能做到这一点?

【问题讨论】:

  • 嗨....好吧...基本上,就像你说的那样。就是这样:-)
  • 所以如果不到一秒那么它应该只点击按钮?
  • 这能回答你的问题吗? How do I create a timer in WPF?

标签: c# wpf button


【解决方案1】:

您应该使用 DispatcherTimer:

using System.Windows.Threading;
...

DispatcherTimer timer = new DispatcherTimer();
timer.Tick += TimerTick;
timer.Interval = TimeSpan.FromSeconds(1);
timer.Start();
...

private void TimerTick(object sender, EventArgs e)
{
    // Put some code here
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-01
    • 2011-02-17
    • 2012-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多