在触摸坐标处获取UI元素

时间:2021-08-18 22:44:01

I'm sure I've read that there is a way of getting co-ordinates on TouchDown in a WPF app and finding out what UI elements are 'underneath' this touch. Can anyone help?

我确信我已经读到有一种方法可以在WPF应用程序中获得TouchDown上的坐标,并找出这些触摸下面的UI元素。有人可以帮忙吗?

2 个解决方案

#1


0  

You have to do a hit test against the visual tree.

你必须对可视树进行命中测试。

Here is an example with mouse click (but touch is more or less the same thing in this respect):

这是一个鼠标点击的例子(但在这方面,触摸或多或少是相同的):

// 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.
    } 
}

Other overload of HitTest can give you multiple hit visuals.

HitTest的其他重载可以为您提供多个视觉效果。

#2


0  

Let your UI elements extend UIElement class like this:

让你的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 should hold the UI elements that raised the touch event, if you need to cancel the manipulation for specific elements you just need to call e.Cancel();

如果你需要取消对你需要调用e.Cancel()的特定元素的操作,involvedUIElement应该保存引发触摸事件的UI元素。

I hope this will help!

我希望这个能帮上忙!

#1


0  

You have to do a hit test against the visual tree.

你必须对可视树进行命中测试。

Here is an example with mouse click (but touch is more or less the same thing in this respect):

这是一个鼠标点击的例子(但在这方面,触摸或多或少是相同的):

// 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.
    } 
}

Other overload of HitTest can give you multiple hit visuals.

HitTest的其他重载可以为您提供多个视觉效果。

#2


0  

Let your UI elements extend UIElement class like this:

让你的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 should hold the UI elements that raised the touch event, if you need to cancel the manipulation for specific elements you just need to call e.Cancel();

如果你需要取消对你需要调用e.Cancel()的特定元素的操作,involvedUIElement应该保存引发触摸事件的UI元素。

I hope this will help!

我希望这个能帮上忙!