在触摸坐标获取UI元素

问题描述:

我确定我已经读过,有一种方法可以在WPF应用程序中获取TouchDown的坐标,并找出触摸屏幕下方的“UI元素”。谁能帮忙?在触摸坐标获取UI元素

+0

为什么你想知道用户界面元素是'底下'这个接触?你需要排除操纵吗? 你什么时候需要它?在操纵三角洲? – simo

+0

我这样做,我一直想弄清楚如何从页面滑动操作中排除我的按钮。 *叹*看到这里:http://goo.gl/pCdVSr – Jordan

您必须对视觉树做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其他重载可以给你多打的视觉效果。

+0

什么是myCanvas在这个例子? – Matt

+0

如果我只想从操作过滤出内容怎么办? http://goo.gl/pCdVSr – Jordan

让你的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();

特定元素的操作

我希望这会有所帮助!