c# - 适用于 Windows Phone(8.1 和 10)平台的 Xamarin Forms 中的滑动手势识别器

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

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

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

最佳答案

I see a lot of examples for renderers that do that for Android and iOS platform. But not for WinRT or 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;
      }
  }

关于c# - 适用于 Windows Phone(8.1 和 10)平台的 Xamarin Forms 中的滑动手势识别器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43549629/

相关文章:

c# - 无法将文件上传到 Azure 存储 C#?

c# - 如何将 Image.Source 获取为字符串

c# - 编译 xamarin android (mono for android) 应用程序时不断出现此错误

c# - 无法关闭 MySQL 数据库连接

c# - 保存和读取 .txt 文件的特定部分

c# - 将段落内的超链接绑定(bind)到命令 (MVVM)

c# - 单击后放大图像 C# UWP

user-interface - uwp 命令栏总是显示标签

c# - 在 CaSTLe Windsor 中注册同一接口(interface)的多个实现

ios - 我如何将内容置于 View Controller 的中心,以便无论 xamarin Storyboard 中使用什么设备尺寸,它始终居中?