如何在平移手势识别器中访问我的总 x 翻译并从 XAML 访问它

How to access my total x translation within a pan gesture recognizer and access it from XAML

我正在设置一个平移手势识别器,当用两根手指平移时,它会移动其中包含的视图。我想在视图在 X 方向上移动指定量时提供事件触发器,这需要每次在手势识别器 class 中更改时将总位移发送到我的 XAML 页面。我无法访问手势识别器之外的总 x 位移 class。

我曾尝试使用委托和事件处理程序从 class 中获取 .TranslationX 或 TotalX 值,但似乎无法弄清楚如何操作。我尝试的每种方法都不会在平移视图时更新值。下面是我的手势容器 class。每次在 class.

之外更改时,我只需要访问总 x 位移
public class GestureContainer : ContentView
{
    public GestureContainer()
    {
        var panGesture = new PanGestureRecognizer();
        panGesture.TouchPoints = 2;
        panGesture.PanUpdated += OnPanUpdated;
        GestureRecognizers.Add(panGesture);
    }

    void OnPanUpdated(object sender, PanUpdatedEventArgs e)
    {
        switch (e.StatusType)
        {
            case GestureStatus.Running:
                Content.TranslationX =
                e.TotalX;
                break;

            case GestureStatus.Completed:
                ViewExtensions.TranslateTo(Content, 0, 0, 250, null);
                break;
        } 
    }
}

我是 C# 和 Xamarin 的新手,如果解决方案显而易见,我深表歉意。我只是不确定如何处理这个问题。

I want to provide event triggers when the view has moved a specified amount in the X direction, which requires the total displacement to be sent to my XAML page every time it is changed in the gesture recognizer class. I am having trouble accessing the total x displacement outside of the gesture recognizer class.

如果你想在移动视图后得到视图TranslationX和TranslationY,我做了一个例子你可以看看:

首先,您可以创建一个平移容器,其中包含一个执行自由形式平移的通用助手 class。

public class PanContainer : ContentView
{
    public double x { get; set; }
    public double y { get; set; }


    public PanContainer ()
    {
        // Set PanGestureRecognizer.TouchPoints to control the 
        // number of touch points needed to pan
        var panGesture = new PanGestureRecognizer ();
        panGesture.PanUpdated += OnPanUpdated;
        GestureRecognizers.Add (panGesture);
    }

    void OnPanUpdated (object sender, PanUpdatedEventArgs e)
    {
        switch (e.StatusType) {

        case GestureStatus.Running:
            // Translate and ensure we don't pan beyond the wrapped user interface element bounds.
            Content.TranslationX = Math.Max (Math.Min (0, x + e.TotalX), -Math.Abs (Content.Width - App.ScreenWidth));
            Content.TranslationY = Math.Max (Math.Min (0, y + e.TotalY), -Math.Abs (Content.Height - App.ScreenHeight));    
            break;

        case GestureStatus.Completed:
            // Store the translation applied during the pan
            x = Content.TranslationX;
            y = Content.TranslationY;
                Console.WriteLine("TranslationX is {0}",x);
                Console.WriteLine("Translationy is {0}", y);
                break;
        }
    }
}

然后显示PanContainer包裹了一个元素,这里我用的是Image元素。

<ContentPage.Content>
    <AbsoluteLayout>
        <local:PanContainer x:Name="pan1">
            <Image
                x:Name="image1"
                HeightRequest="1024"
                Source="MonoMonkey.jpg"
                WidthRequest="768" />

        </local:PanContainer>

        <Label x:Name="labelx" Text="{Binding Source={x:Reference image1}, Path=TranslationX}" />
        <Label
            x:Name="labely"
            Margin="30"
            Text="{Binding Source={x:Reference image1}, Path=TranslationY}" />
    </AbsoluteLayout>
</ContentPage.Content>

您可以使用 bind 获取图像 TranslationX 和 TranslationY。