如何启用滑动手势以在Xamarin表单UWP中引入母版页?

时间:2017-07-19 08:14:59

标签: ios uwp xamarin.forms

我有一个iOS和UWP的Xamarin表单项目,已经实现了主 - 细节布局,它在两个平台上都很好用,但是在iOS上如果我们从左边滑动,将显示母版页(汉堡菜单),但是在UWP我们需要单击菜单图标以显示母版页,如何启用滑动以在UWP上显示母版页?

1 个答案:

答案 0 :(得分:2)

目前,Xamarin.Forms没有这样的“SwipeGestureRecognizer”API。但您可以根据PanGestureRecognizer自定义SwipeGestureRecognizer。我编写了以下用于模拟“SwipeGestureRecognizer”的代码。但我使用的阈值仅用于测试,而不是真实的阈值,您可以根据您的要求修改阈值。

public enum SwipeDeriction
{
    Left = 0,
    Rigth,
    Above,
    Bottom
}

public class SwipeGestureReconginzer : PanGestureRecognizer
{
    public delegate void SwipeRequedt(object sender, SwipeDerrictionEventArgs e);

    public event EventHandler<SwipeDerrictionEventArgs> Swiped;

    public SwipeGestureReconginzer()
    {
        this.PanUpdated += SwipeGestureReconginzer_PanUpdated;
    }

    private void SwipeGestureReconginzer_PanUpdated(object sender, PanUpdatedEventArgs e)
    {
        if (e.TotalY > -5 | e.TotalY < 5)
        {
            if (e.TotalX > 10)
            {
                Swiped(this, new SwipeDerrictionEventArgs(SwipeDeriction.Rigth));
            }
            if (e.TotalX < -10)
            {
                Swiped(this, new SwipeDerrictionEventArgs(SwipeDeriction.Left));
            }
        }

        if (e.TotalX > -5 | e.TotalX < 5)
        {
            if (e.TotalY > 10)
            {
                Swiped(this, new SwipeDerrictionEventArgs(SwipeDeriction.Bottom));
            }
            if (e.TotalY < -10)
            {
                Swiped(this, new SwipeDerrictionEventArgs(SwipeDeriction.Above));
            }
        }
    }
}

public class SwipeDerrictionEventArgs : EventArgs
{
    public SwipeDeriction Deriction { get; }

    public SwipeDerrictionEventArgs(SwipeDeriction deriction)
    {
        Deriction = deriction;
    }
}

<强> MainPage.xaml.cs中

var swipe = new SwipeGestureReconginzer();
swipe.Swiped += Tee_Swiped;
TestLabel.GestureRecognizers.Add(swipe);

private void Tee_Swiped(object sender, SwipeDerrictionEventArgs e)
{
    switch (e.Deriction)
      {
          case SwipeDeriction.Above:
              {
              }
              break;

          case SwipeDeriction.Left:
              {
              }
              break;

          case SwipeDeriction.Rigth:
              {
              }
              break;

          case SwipeDeriction.Bottom:
              {
              }
              break;

          default:
              break;
     }
}