WPF:按钮单击+ 双击问题
我必须处理 WPF 应用程序中按钮的单击和双击,并具有不同的反应。 不幸的是,在双击时,WPF 会触发两个单击事件和一个双击事件,因此很难处理这种情况。
它试图使用计时器来解决它,但没有成功......我希望你能帮助我。
让我们看一下代码:
private void delayedBtnClick(object statInfo)
{
if (doubleClickTimer != null)
doubleClickTimer.Dispose();
doubleClickTimer = null;
this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new VoidDelegate(delegate()
{
// ... DO THE SINGLE CLICK ACTION
}));
}
private void btn_Click(object sender, RoutedEventArgs e)
{
if (doubleClickTimer == null)
doubleClickTimer = new Timer(delayedBtnClick, null, System.Windows.Forms.SystemInformation.DoubleClickTime, Timeout.Infinite);
}
}
}
private void btnNext_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if (doubleClickTimer != null)
doubleClickTimer.Change(Timeout.Infinite, Timeout.Infinite); // disable it - I've tried it with and without this line
doubleClickTimer.Dispose();
doubleClickTimer = null;
//.... DO THE DOUBLE CLICK ACTION
}
问题是在双击时“双击操作”之后调用了“单击操作”。 奇怪的是,我在双击时将doubleClickTimer
设置为null,但在delayedBtnClick
中它是true:O
我已经尝试使用更长的时间、布尔标志和锁定。 ..
你有什么想法?
最好的!
I have to handle both the single click and the double click of a button in a WPF application with different reaction.
Unfortunately, on a doubleclick, WPF fires two click event and a double click event, so it's hard to handle this situation.
It tried to solve it using a timer but without success...I hope you can help me.
Lets see the code:
private void delayedBtnClick(object statInfo)
{
if (doubleClickTimer != null)
doubleClickTimer.Dispose();
doubleClickTimer = null;
this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new VoidDelegate(delegate()
{
// ... DO THE SINGLE CLICK ACTION
}));
}
private void btn_Click(object sender, RoutedEventArgs e)
{
if (doubleClickTimer == null)
doubleClickTimer = new Timer(delayedBtnClick, null, System.Windows.Forms.SystemInformation.DoubleClickTime, Timeout.Infinite);
}
}
}
private void btnNext_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if (doubleClickTimer != null)
doubleClickTimer.Change(Timeout.Infinite, Timeout.Infinite); // disable it - I've tried it with and without this line
doubleClickTimer.Dispose();
doubleClickTimer = null;
//.... DO THE DOUBLE CLICK ACTION
}
The problem is that the 'SINGLE CLICK ACTION' called after the 'DOUBLE CLICK ACTION' on doubleclick. It's strange that I set thedoubleClickTimer
to null on double click but in the delayedBtnClick
it's true :O
I've already tried to use longer time, a bool flag and lock...
Do you have any ideas?
Best!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果在处理
MouseDoubleClick
事件后将RoulatedEvent
的e.Handled
设置为true
,那么它将不会调用MouseDoubleClick
之后第二次发生Click
事件。有一篇最近的帖子涉及不同的行为对于
SingleClick
和DoubleClick
可能有用。但是,如果您确定想要单独的行为并且想要/需要阻止第一次
Click
以及第二次Click
,则可以使用DispatcherTimer
代码> 就像你一样。If you set the
RoutedEvent
'se.Handled
totrue
after handling theMouseDoubleClick
event then it will not call theClick
Event the second time after theMouseDoubleClick
.There's a recent post which touches on having different behaviors for
SingleClick
andDoubleClick
which may be useful.However, if you are sure you want separate behaviors and want/need to block the first
Click
as well as the secondClick
, you can use theDispatcherTimer
like you were.您可以尝试以下操作:
如果需要,您可能需要单击鼠标并等待鼠标松开才能执行操作。
You could try this:
If neccessary, you could require mouse click and wait until mouse up to perform the action.