当我具有触摸事件处理程序时,为什么我的鼠标事件处理程序不起作用? [英] Why my Mouse Event Handlers are not working when I have Touch Event Handlers?

查看:121
本文介绍了当我具有触摸事件处理程序时,为什么我的鼠标事件处理程序不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的某些最终用户使用触摸屏,而其他用户则使用PC.在触摸屏上,PreviewMouseUp/Down与触摸事件处理程序一起触发,从而导致重复的行为(用PreviewMousUp/Down编写的函数被执行两次).

Some of my end users have touch screens, and others have PCs. On touch screens, PreviewMouseUp/Down fire along with the touch event handlers, causing duplicate behavior (functions written in PreviewMousUp/Down get executed twice).

所以我的示例Button XAML:

So my sample Button XAML:

<Button x:Name="Whatever" Background="Transparent"  MouseUp="Whatever_MouseUp" MouseDown="Whatever_MouseDown" TouchUp="Whatever_TouchUp" TouchDown="Whatever_TouchDown">
    <StackPanel>
        <TextBlock x:Name="WhateverText" Text="Soemthing" FontSize="13"/>
        <Image x:Name="WhateverImage" Source="bla/bla/bla"/>
    </StackPanel>
</Button>

为什么在PC上没有触发MouseDownMouseUp事件处理程序?

Why MouseDown and MouseUp event handlers are not getting fired on the PC?

如果我在触摸屏上执行,它的工作原理就像一个超级按钮(Touch事件处理程序).但是,在我的PC(VS-2015)上根本无法正常工作.拜托了,谢谢

If I execute on a touch screen, it works like a charm (Touch event handlers). However, on my PC(VS-2015) it doesn't work at all. Please and thanks

推荐答案

似乎Click事件处理程序阻止了MouseDown和MouseUp的触发事件.我希望这是因为,当我从头开始构建自定义控件(如按钮)时,会使用这些事件来触发Click事件. 这只是我的期望.

It seems that the Click event handler prevents firing events of MouseDown and MouseUp. I expect that because when I build custom controls like buttons from scratch, I use these events to fire Click event. This is my expectation only.

无论如何,我尝试过,如果您未实现TouchDown/Up,则在触摸和非触摸上均会触发PreviewMouseDown/Up.但是,如果您使用它们实现了TouchDown/Up,则在TOUCH上的执行将如下所示:> TouchDown> PreviewMouseDown> TouchUp> PreviewMouseUp.在NON-TOUCH上的执行将是这样的:> PreviewMouseDown> PreviewMouseUp.因此,我建议您使用PreviewMouseDown/Up,因为它同时适用于触摸式和非触摸式.

Anyway, I tried it and PreviewMouseDown/Up is fired on both touch and non-touch if you didn't implemented TouchDown/Up. But if you implemented TouchDown/Up with them, execution on TOUCH will be like this: > TouchDown > PreviewMouseDown > TouchUp > PreviewMouseUp. And execution on NON-TOUCH will be like this: > PreviewMouseDown > PreviewMouseUp. So I suggest on you to use PreviewMouseDown/Up because it works for both touch and non-touch.

还可以使用MouseRightButtonDown/Up它将与您一起使用.但是您会注意到,它之后会触发MouseDown/Up.您可以通过添加e.Handled = true来简单地防止这种情况.在MouseRightButtonDown/Up处理程序中.

Also you can use MouseRightButtonDown/Up it will work with you. But you will note that MouseDown/Up is fired after it. You can simply prevent that by adding e.Handled = true; inside MouseRightButtonDown/Up handlers.

尝试利用这些提示,如果您无法解决,请告诉我,我会与您一起考虑的.祝你好运.

Try to make use of these hints and if you couldn't solve it just tell me and I'll think with you. Good luck.

这篇关于当我具有触摸事件处理程序时,为什么我的鼠标事件处理程序不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆