【发布时间】:2017-12-24 09:05:22
【问题描述】:
我有一个带有 iOS 和 UWP 的 Xamarin 表单项目,已经实现了主从布局,它在两个平台上都运行良好,但在 iOS 上,如果我们从左侧滑动,将显示母版页(汉堡菜单),但在UWP我们需要点击菜单图标调出母版页,如何在UWP上启用滑动显示母版页?
【问题讨论】:
标签: ios uwp xamarin.forms
我有一个带有 iOS 和 UWP 的 Xamarin 表单项目,已经实现了主从布局,它在两个平台上都运行良好,但在 iOS 上,如果我们从左侧滑动,将显示母版页(汉堡菜单),但在UWP我们需要点击菜单图标调出母版页,如何在UWP上启用滑动显示母版页?
【问题讨论】:
标签: ios uwp xamarin.forms
目前,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;
}
}
【讨论】: