0
votes

I have a Xamarin forms project with iOS and UWP, have implemented master-detail layout and it works great on both the platforms, but on iOS if we swipe from left, master page(Hamburger menu) will be displayed, but on UWP we need to click on the menu icon to bring up master page, how can I enable swipe to display the master page on UWP?

1

1 Answers

2
votes

Currently, there is no such "SwipeGestureRecognizer" api for Xamarin.Forms. But you could custom SwipeGestureRecognizer base on PanGestureRecognizer. I have written the following code for simulating "SwipeGestureRecognizer". But the threshold I used is just for testing and not the real one, you can modify the threshold based on your requirement.

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;
     }
}