【问题标题】:How to enable swipe gesture to bring master page in Xamarin forms UWP?如何启用滑动手势以在 Xamarin 表单 UWP 中显示母版页?
【发布时间】:2017-12-24 09:05:22
【问题描述】:

我有一个带有 iOS 和 UWP 的 Xamarin 表单项目,已经实现了主从布局,它在两个平台上都运行良好,但在 iOS 上,如果我们从左侧滑动,将显示母版页(汉堡菜单),但在UWP我们需要点击菜单图标调出母版页,如何在UWP上启用滑动显示母版页?

【问题讨论】:

    标签: ios uwp xamarin.forms


    【解决方案1】:

    目前,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;
         }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-04-08
      • 1970-01-01
      • 2018-03-25
      • 2017-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-03
      • 2018-01-14
      相关资源
      最近更新 更多