父级的“触摸移动"事件正常工作时,“触摸移动"事件不适用于孩子 [英] Touch Move events not working for child when Touch Move event of parent is working
本文介绍了父级的“触摸移动"事件正常工作时,“触摸移动"事件不适用于孩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当在窗口上移动触摸时,会调用窗口的触摸移动",但是当触摸移动到达子级(椭圆形)"时
When Moving touch on Window Touch Move of Window is called but when touch move reaches Child (Ellipse)
未触发Touch Move事件,但我至少要确定Touch是否在Ellipse上.因为它可以在鼠标移动时起作用
Touch Move event is not fired But I atleast I want to identify if the Touch is on Ellipse or not. as it it works on mouse move
这是我的Xaml代码
<Window x:Class="TouchEvent.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" IsManipulationEnabled="True" TouchMove="Window_TouchMove">
<Canvas Name="MainCanvas" Height="350" Width="525" Background="Gray" >
<!--
The ellipse that will be dragged around.
The Top and Left attributes must be set
or Canvas.GetTop and Canvas.GetLeft will return NaN.
-->
<Ellipse IsManipulationEnabled="True"
Name="DragEllipse"
Canvas.Top="0" Canvas.Left="0"
Fill="Red"
Width="100" Height="100"
TouchDown="DragEllipse_TouchDown"
TouchMove="DragEllipse_TouchMove"
TouchLeave="DragEllipse_TouchLeave"
>
</Ellipse>
</Canvas>
</Window>
这是我的.CS代码
// Declare the global variables.
TouchDevice ellipseControlTouchDevice;
Point lastPoint;
private void DragEllipse_TouchDown(object sender, TouchEventArgs e)
{
// Capture to the ellipse.
e.TouchDevice.Capture(this.DragEllipse);
// Remember this contact if a contact has not been remembered already.
// This contact is then used to move the ellipse around.
if (ellipseControlTouchDevice == null)
{
ellipseControlTouchDevice = e.TouchDevice;
// Remember where this contact took place.
lastPoint = ellipseControlTouchDevice.GetTouchPoint(this.MainCanvas).Position;
}
// Mark this event as handled.
e.Handled = true;
}
private void DragEllipse_TouchMove(object sender, TouchEventArgs e)
{
if (e.TouchDevice == ellipseControlTouchDevice)
{
// Get the current position of the contact.
Point currentTouchPoint = ellipseControlTouchDevice.GetTouchPoint(this.MainCanvas).Position;
// Get the change between the controlling contact point and
// the changed contact point.
double deltaX = currentTouchPoint.X - lastPoint.X;
double deltaY = currentTouchPoint.Y - lastPoint.Y;
// Get and then set a new top position and a new left position for the ellipse.
double newTop = Canvas.GetTop(this.DragEllipse) + deltaY;
double newLeft = Canvas.GetLeft(this.DragEllipse) + deltaX;
Canvas.SetTop(this.DragEllipse, newTop);
Canvas.SetLeft(this.DragEllipse, newLeft);
// Forget the old contact point, and remember the new contact point.
lastPoint = currentTouchPoint;
// Mark this event as handled.
e.Handled = true;
}
}
private void DragEllipse_TouchLeave(object sender, TouchEventArgs e)
{
// If this contact is the one that was remembered
if (e.TouchDevice == ellipseControlTouchDevice)
{
// Forget about this contact.
ellipseControlTouchDevice = null;
}
// Mark this event as handled.
e.Handled = true;
}
private void Window_TouchMove(object sender, TouchEventArgs e)
{
if (e.Source.GetType() != typeof(TouchEvent.MainWindow))
{
}
}
}
}
推荐答案
我不确定您提到的实际问题是什么,看起来您的代码在Simulator中运行正常:
I'm not sure what the actual issue you mentioned is, it looks like that your code is working fine in the Simulator:
当我触摸椭圆外部的区域时, Window_TouchMove 可以触发事件:
When I touched the area outside the Ellipse, the Window_TouchMove event can be fired:
这篇关于父级的“触摸移动"事件正常工作时,“触摸移动"事件不适用于孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文