在触摸坐标获取UI元素
答
您必须对视觉树做hit test。
下面是一个例子鼠标点击(但触摸或多或少在这方面同样的事情):
// Respond to the left mouse button down event by initiating the hit test.
private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
// Retrieve the coordinate of the mouse position.
Point pt = e.GetPosition((UIElement)sender);
// Perform the hit test against a given portion of the visual object tree.
HitTestResult result = VisualTreeHelper.HitTest(myCanvas, pt);
if (result != null)
{
// Perform action on hit visual object.
}
}
的HitTest其他重载可以给你多打的视觉效果。
答
让你的UI元素扩展的UIElement类是这样的:
class MyUIElement : UIElement
{
protected override void OnManipulationStarting(System.Windows.Input.ManipulationStartingEventArgs e)
{
base.OnManipulationStarting(e);
UIElement involvedUIElement = e.Source as UIElement;
// to cancel the touch manipulaiton:
e.Cancel();
}
}
involvedUIElement应该认为提出的触摸事件的UI元素,如果你需要取消你只需要调用e.Cancel();
我希望这会有所帮助!
为什么你想知道用户界面元素是'底下'这个接触?你需要排除操纵吗? 你什么时候需要它?在操纵三角洲? – simo
我这样做,我一直想弄清楚如何从页面滑动操作中排除我的按钮。 *叹*看到这里:http://goo.gl/pCdVSr – Jordan