【问题标题】:Swipe gesture recognizer in Xamarin Forms for windows phone (8.1 & 10) platform适用于 windows phone (8.1 & 10) 平台的 Xamarin Forms 中的滑动手势识别器
【发布时间】:2017-09-18 20:45:16
【问题描述】:

如何在 Windows Phone(8.1 和 10)平台的 Xamarin Forms 中最好地实现滑动 gesture recognizer?

我看到很多关于renderers 的示例都适用于 Android 和 iOS 平台。但不适用于 WinRT 或 UWP。

【问题讨论】:

    标签: c# xamarin uwp xamarin.forms windows-runtime


    【解决方案1】:

    我看到很多针对 Android 和 iOS 平台的渲染器示例。但不适用于 WinRT 或 UWP。

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

    【讨论】:

    • 我还不能测试这种方法——但我真的很喜欢它。我也许可以为大多数 UI 控件添加滑动手势支持,而无需为它们编写平台渲染器。
    • 我能看到的唯一警告是只有在滑动完成时才能检测和触发事件 - 但通过对阈值和计时器进行轻微调整,我想我将能够管理它。谢谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-20
    • 1970-01-01
    • 2015-07-10
    • 2016-08-04
    相关资源
    最近更新 更多