我想禁用一个按钮 - 以防止双击:在平板电脑上你按下一次,它会点击两次,这是我所知道的最简单的 hack - 短期但我注意到/调试间隔在实践中可能太长; 50 毫秒与 > 2 秒。
只有一行启动计时器,一行停止计时器。随机间隔为 50 毫秒或更大。没有 CPU 消耗,我只需在我的 4 核台式机上用鼠标单击按钮。
会是什么原因呢?
DispatcherTimer timerTouchDelay = new DispatcherTimer();
protected override void OnMouseDown(MouseButtonEventArgs e)
{
//Init
if (timerTouchDelay.Interval.Milliseconds == 0)
{
timerTouchDelay.Tick += new EventHandler(timerTouchDelay_Tick);
timerTouchDelay.Interval = new TimeSpan(0, 0, 0, 0, 50); //ms
}
if(timerTouchDelay.IsEnabled)
return;
timerTouchDelay.Start();
HandleKeyDown();
base.OnMouseDown(e);
}
private void timerTouchDelay_Tick(object sender, EventArgs e)
{
timerTouchDelay.Stop();
}
请您参考如下方法:
要了解为什么会这样,我强烈推荐以下文章:
Comparing the Timer Classes in the .NET Framework Class Library
作为引用,DispatcherTimer 与 System.Windows.Forms.Timer 非常相似,对于后者,作者指出“如果您正在寻找节拍器,您我来错地方了”。此计时器并非设计为以精确的时间间隔“滴答”。
