Xamarin:获取某个元素内是否有手势触摸

Xamarin: get if a pangesture touch is inside a certain element

有没有办法检查某个元素是否在某个元素内,例如 Frame

我在屏幕上添加了 PanGestureRecognizer。在我的屏幕上,我有两个 Frame,其中包含两个 Image。我想要的是,当我在屏幕上移动手指时,当我的手指停在其中一个帧上时,我会被注意到。我知道如何获取我的手指坐标 (e.TotalX; e.TotalY;),我的想法是检查它们是否在框架的范围内,但我不知道如何。

有什么建议吗?

代码

public MainPage()
    {
        InitializeComponent();

        var panGesture = new PanGestureRecognizer();
        panGesture.PanUpdated += PanUpdated;
        mainLayout.GestureRecognizers.Add(panGesture);

        dog = new Frame
        {
            BorderColor = Color.Blue,
            Padding = 4,
            Content = new Image
            {
                Source = ImageSource.FromResource("AccessibilityImagesSound.Immagini.dog.png"),
                Aspect = Aspect.Fill,
            },

        };
        AutomationProperties.SetIsInAccessibleTree(dog, true);
        AutomationProperties.SetName(dog, "dog");
        AbsoluteLayout.SetLayoutBounds(dog, new Rectangle(0.2, 0.5, 0.30, 0.30));
        AbsoluteLayout.SetLayoutFlags(dog, AbsoluteLayoutFlags.All);
        mainLayout.Children.Add(dog);



        cat = new Frame
        {
            BorderColor = Color.Blue,
            Padding = 4,
            Content = new Image
            {
                Source = ImageSource.FromResource("AccessibilityImagesSound.Immagini.cat.png"),
                Aspect = Aspect.Fill,
            },

        };
        AutomationProperties.SetIsInAccessibleTree(cat, true);
        AutomationProperties.SetName(cat, "cat");
        AbsoluteLayout.SetLayoutBounds(cat, new Rectangle(0.8, 0.5, 0.30, 0.30));
        AbsoluteLayout.SetLayoutFlags(cat, AbsoluteLayoutFlags.All);
        mainLayout.Children.Add(cat);

    }

    private void PanUpdated(object sender, PanUpdatedEventArgs e)
    {
        switch (e.StatusType)
        {
            case GestureStatus.Started:

                break;
            case GestureStatus.Running:

                //Here I think I have to check E.TotalX and e.TotalY

                break;
            case GestureStatus.Completed:
            case GestureStatus.Canceled: 
                break;
        }
    }

据我所知,在 xamarin 中获取 Touch 位置的唯一方法是通过本机触摸手势。

可以使用 Effects 使用本机手势,this MSDocs link 已完全实现。

使用 TouchEffect 可以轻松获取触摸的确切位置。下面给出了改变触摸框颜色的示例。

XAML:

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid.Effects>
            <touch:TouchEffect TouchAction="Grid_TouchAction"/>
        </Grid.Effects>
        <Frame
            Grid.Row="1"
            BackgroundColor="Green"
            x:Name="leftFrame"/>
        <Frame
            Grid.Row="1"
            Grid.Column="1"
            BackgroundColor="Blue"
            x:Name="rightFrame"/>
    </Grid>

CS :

    private void Grid_TouchAction(object sender, TouchTracking.TouchActionEventArgs args)
    {
        switch (args.Type)
        {
            case TouchActionType.Moved:
                if (leftFrame.Bounds.Contains(args.Location))
                {
                    leftFrame.BackgroundColor = Color.Red;
                }
                else
                {
                    leftFrame.BackgroundColor = Color.Green;
                }

                if (rightFrame.Bounds.Contains(args.Location))
                {
                    rightFrame.BackgroundColor = Color.Red;
                }
                else
                {
                    rightFrame.BackgroundColor = Color.Blue;
                }
                break;
        }
    }

UI 工作:

根据您的要求更改 TouchAction 事件处理程序。