简体   繁体   English

当我具有触摸事件处理程序时,为什么我的鼠标事件处理程序不起作用?

[英]Why my Mouse Event Handlers are not working when I have Touch Event Handlers?

Some of my end users have touch screens, and others have PCs. 我的某些最终用户有触摸屏,而其他一些则有PC。 On touch screens, PreviewMouseUp/Down fire along with the touch event handlers, causing duplicate behavior (functions written in PreviewMousUp/Down get executed twice). 在触摸屏上, PreviewMouseUp/Down与触摸事件处理程序一起触发,从而导致重复行为(用PreviewMousUp / Down编写的函数执行两次)。

So my sample Button XAML: 所以我的示例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>

Why MouseDown and MouseUp event handlers are not getting fired on the PC? 为什么在PC上没有触发MouseDownMouseUp事件处理程序?

If I execute on a touch screen, it works like a charm (Touch event handlers). 如果我在触摸屏上执行,它的工作原理就像一个超级按钮(Touch事件处理程序)。 However, on my PC(VS-2015) it doesn't work at all. 但是,在我的PC(VS-2015)上根本不起作用。 Please and thanks 请和谢谢

It seems that the Click event handler prevents firing events of MouseDown and MouseUp. 似乎Click事件处理程序可防止触发MouseDown和MouseUp事件。 I expect that because when I build custom controls like buttons from scratch, I use these events to fire Click event. 我希望这是因为,当我从头开始构建自定义控件(如按钮)时,会使用这些事件来触发Click事件。 This is my expectation only. 这只是我的期望。

Anyway, I tried it and PreviewMouseDown/Up is fired on both touch and non-touch if you didn't implemented TouchDown/Up. 无论如何,我尝试了一下,如果您未实现TouchDown / Up,则在触摸和非触摸上均会触发PreviewMouseDown / Up。 But if you implemented TouchDown/Up with them, execution on TOUCH will be like this: > TouchDown > PreviewMouseDown > TouchUp > PreviewMouseUp. 但是,如果您使用它们实现了TouchDown / Up,则在TOUCH上的执行将如下所示:> TouchDown> PreviewMouseDown> TouchUp> PreviewMouseUp。 And execution on NON-TOUCH will be like this: > PreviewMouseDown > PreviewMouseUp. 在NON-TOUCH上的执行将如下所示:> PreviewMouseDown> PreviewMouseUp。 So I suggest on you to use PreviewMouseDown/Up because it works for both touch and non-touch. 因此,我建议您使用PreviewMouseDown / Up,因为它适用于触摸和非触摸。

Also you can use MouseRightButtonDown/Up it will work with you. 您也可以使用MouseRightButtonDown / Up与您一起使用。 But you will note that MouseDown/Up is fired after it. 但是您会注意到,它之后会触发MouseDown / Up。 You can simply prevent that by adding e.Handled = true; 您可以通过添加e.Handled = true来防止这种情况。 inside MouseRightButtonDown/Up handlers. 在MouseRightButtonDown / Up处理程序中。

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. 祝好运。

I am unsure as to why you are not simply using the Click event which should work for both mouse and touch, but the reason you are not seeing MouseUp and MouseDown is explained here: WPF MouseLeftButtonUp Not Firing . 我不确定为什么不简单地使用同时适用于鼠标和触摸的Click事件,但是在这里解释了为什么看不到MouseUp和MouseDown的原因: WPF MouseLeftButtonUp Not Firing Unless you create your own button control and control the container itself or use your own Button Template, PreviewXXX events are your best bet if Click Event is not an option. 除非您创建自己的按钮控件并控制容器本身或使用自己的按钮模板,否则如果没有“单击事件”选项,PreviewXXX事件将是最好的选择。 Even if you set event handlers on a StackPanel in a template, you do not get MouseUp events because the Button will have the mouse captured due to the default behavior of the Button control. 即使您在模板的StackPanel上设置了事件处理程序,您也不会获得MouseUp事件,因为Button控件的默认行为会导致Button捕获鼠标。 The code below will get MouseDown event for StackPanel only but MouseUp will never get fired. 下面的代码仅会为StackPanel获取MouseDown事件,但永远不会触发MouseUp。 Just for verification I added Click event and it was fired every time I released the mouse within the Button, which is expected behavior for Button. 为了进行验证,我添加了Click事件,每次在Button中释放鼠标时都会触发该事件,这是Button的预期行为。

    <Button Click="Whatever_Click" x:Name="Whatever" MouseUp="Whatever_MouseUp" MouseDown="Whatever_MouseDown" TouchUp="Whatever_TouchUp" TouchDown="Whatever_TouchDown">
        <Button.Template>
            <ControlTemplate TargetType="{x:Type Button}">
                <StackPanel x:Name="stackButton" MouseUp="Whatever_MouseUp" MouseDown="Whatever_MouseDown" TouchUp="Whatever_TouchUp" TouchDown="Whatever_TouchDown">
                    <TextBlock x:Name="WhateverText" Text="StackPanelClick" FontSize="13"/>
                    <Image x:Name="WhateverImage" Source="dice.png"/>
                </StackPanel>
            </ControlTemplate>
        </Button.Template>
    </Button>

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

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