简体   繁体   English

如何在同一按钮中使用“鼠标”和“触摸”事件?

[英]How can I use Mouse and Touch events in the same button?

I have a button with Click="Window_Click" and TouchDown="Window_Touch" . 我有一个带有Click="Window_Click"TouchDown="Window_Touch"的按钮。 When I click in non touch devices it works just fine, but when I click in touch devices , sometimes I have another button in focus and the touch trigger two events. 当我在非触摸设备上单击时,它可以正常工作,但是当我在触摸设备上单击时,有时我会聚焦另一个按钮,并且触摸会触发两个事件。 Basicaly when I touch the button, they execute both events, how can I create just one event for both behaviors or how can I "stop"/don't execute clicks events when I touching things? 基本上,当我触摸按钮时,它们将同时执行两个事件,如何为两个行为都创建一个事件,或者在我触摸事物时如何“停止” /不执行点击事件?

private void Window_Click(object sender, RoutedEventArgs e)
{
    functionOne();
}
private void Window_Touch(object sender, TouchEventArgs e)
{
    functionOne();
}

You need to catch and handle duplicate calls to the event. 您需要捕获并处理对该事件的重复调用。 You could try setting a handled boolean so the click event knows if the touch event handled the event or not (the touch event should be firing first). 您可以尝试设置一个已处理的布尔值,以便click事件知道touch事件是否处理了该事件(应首先触发touch事件)。 Something like this... 像这样

private bool handled = false;
private void Window_Click(object sender, RoutedEventArgs e)
{
    if(!handled)
    functionOne();
}
private void Window_Touch(object sender, TouchEventArgs e)
{
    handled = true;
    functionOne();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM