簡體   English   中英

在觸摸坐標處獲取UI元素

[英]Get UI element at the touch co-ordinates

我確信我已經讀過一種方法,可以在WPF應用程序中獲取TouchDown上的坐標,並找出該觸摸“下方”的UI元素。 有人可以幫忙嗎?

您必須對視覺樹進行一次命中測試

這是鼠標單擊的示例(但在這方面觸摸幾乎是相同的):

// 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();
        }
    }

relatedUIElement應該包含引發觸摸事件的UI元素,如果您需要取消對特定元素的操作,則只需調用e.Cancel();

我希望這個能幫上忙!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM