如何在不产生重复行为的情况下组合触摸和鼠标事件处理程序? [英] How can I combine Touch and Mouse Event Handlers without producing duplicate behavior?
问题描述
我正在开发WPF应用程序,该应用程序正在触摸屏平板电脑上使用.我使用VS2015 IDE进行开发,并使用鼠标进行调试.
I am working on a WPF application which is being used on a Touch Screen Tablet. I develop using VS2015 IDE and I use my Mouse to debug.
我必须处理按钮的 down
和 up
事件,才能执行某些任务.我使用 PreviewMouse
和 PreviewTouch
事件处理程序,在每种情况下我都遇到问题:
I must handle buttons' down
and up
events to execute certain tasks. I use PreviewMouse
and PreviewTouch
event handlers and I have a problem in every case I use:
-
案例1:使用
PreviewMouseDown
,PreviewMouseUp
,PreviewTouchDown
和PreviewTouchUp 代码>.对于每个按钮,我需要复制代码以包括单独的Touch和Mouse Event处理程序,但功能完全相同.我这样做是为了能够使用应用程序(鼠标),并让用户使用它(触摸).问题:触摸事件处理程序执行鼠标事件处理程序;导致应用程序复制行为.例如:一个将
x
递增1的按钮,如果您单击"它,则将增加1,而如果您触摸"它,则将增加2.
Case 1: Using
PreviewMouseDown
,PreviewMouseUp
,PreviewTouchDown
andPreviewTouchUp
. For each button, I need to duplicate my code to include separate Touch and Mouse Event handlers but the exact same functionality. I do this for me to be able to use the Application (Mouse) and for the the user to use it (Touch). Problem: Touch Event Handlers executes Mouse Event Handlers; Causing the application to duplicate the behavior. Ex: A button that incrementsx
by one, will increment by one if you "Click" it but increment by two if you "Touch" it.
案例2:使用点击
, PreviewMouseUp
和 PreviewTouchUp
.问题:鼠标单击上没有调用 PreviewTouchUp
和 PreviewMouseUp
.
Case 2: Using Click
, PreviewMouseUp
and PreviewTouchUp
. Problem: The PreviewTouchUp
and PreviewMouseUp
do not get called on Mouse Click.
Case 3: Create a method for each button and call it from both Touch and Mouse events like How to get Touchscreen to use Mouse Events. Problem: Duplicate behavior (method gets called twice)
案例4::删除所有Touch事件,因为在任何Touch上执行 PreviewMouseDown
和 PreviewMouseUp
.该行为有效,但是我需要触摸按钮上的某些位置才能执行.(透明度?我必须精确地触摸一个位置-就像鼠标一样吗?)
Case 4: Remove all Touch events since PreviewMouseDown
and PreviewMouseUp
are being executed on any Touch. The behavior works but I need to Touch certain positions on the button in order for it to execute. (Transparency? I must touch exactly one position - like a mouse?)
案例5:,而不是使用 Preview
使用 MouseUp
和 MouseDown
.根本没有在Touch上工作,没有触摸按钮上的任何位置.
Case 5: Using MouseUp
and MouseDown
instead of Preview
. Didn't work on Touch at all, touching any position on the button.
I want something similar to this Prevent a WPF application to interpret touch events as mouse events but only on the tablet. I still need to use my mouse in my environment. How can I fix that?
按钮示例XAML:
<Button x:Name="locationScrollUpButton" Margin="0,5,5,0" Background="Transparent" Padding="0" Grid.Row="1" Grid.Column="1" PreviewMouseUp="locationScrollUpButton_PreviewMouseUp" PreviewMouseDown="locationScrollUpButton_PreviewMouseDown" BorderThickness="0" PreviewTouchDown="locationScrollUpButton_PreviewTouchDown" PreviewTouchUp="locationScrollUpButton_PreviewTouchUp" >
<StackPanel>
<TextBlock x:Name="locationUpButtonText" Text="Up" FontSize="13" Margin="0,2,0,4" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<Image x:Name="locationUpButtonImage" Source="/ProjectName;component/Resources/CaptureView/Location-Scroll-Up-White.png" Width="55" Height="60"/>
</StackPanel>
</Button>
推荐答案
So I ended up doing it using a cool hack. Based on Ahmad's answer on this other question HERE we know the fire order of the handlers:
在触摸设备上:
TouchDown> PreviewMouseDown> TouchUp> PreviewMouseUp
TouchDown > PreviewMouseDown > TouchUp > PreviewMouseUp
非接触式:
PreviewMouseDown> PreviewMouseUp
PreviewMouseDown > PreviewMouseUp
并基于此处的@ M.Kazem评论.现在,此修复程序如下所示:
And based on @M.Kazem comments here. The fix now looks something like this:
private bool ButtonPreview = true;
private void Button_TouchDown(object sender, TouchEventArgs e)
{
// task 1
ButtonPreview = false;
}
private void Button_TouchUp(object sender, TouchEventArgs e)
{
// task 2
// ..
}
private void Button_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
if (ButtonXPreview)
{
// task 1
}
else
ButtonXPreview = true;
}
private void Button_PreviewMouseUp(object sender, MouseButtonEventArgs e)
{
if (ButtonXPreview)
{
// task 2
}
else
ButtonXPreview = true;
}
这篇关于如何在不产生重复行为的情况下组合触摸和鼠标事件处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!