在桌面模式下,UWP 应用程序是一个窗口应用程序,难以捕捉屏幕边缘滑动事件,EdgeGesture 可能在平板电脑模式下工作。
为了适应不同情况的滑动需求,可以考虑使用GestureRecognizer。
这里我给你一个简单的页面供你参考。
<Page ...>
<Grid Background="White">
</Grid>
</Page>
private GestureRecognizer _GestureRecognizer;
PointerEventHandler PointerPressedEventHandler;
PointerEventHandler PointerMovedEventHandler;
PointerEventHandler PointerReleasedEventHandler;
PointerEventHandler PointerCanceledEventHandler;
private double startY = 0;
private double moveY = 0;
public GesturePage()
{
this.InitializeComponent();
_GestureRecognizer = new GestureRecognizer();
_GestureRecognizer.GestureSettings = GestureSettings.ManipulationTranslateY;
_GestureRecognizer.ManipulationStarted += _GestureRecognizer_ManipulationStarted;
_GestureRecognizer.ManipulationUpdated += _GestureRecognizer_ManipulationUpdated;
_GestureRecognizer.ManipulationCompleted += _GestureRecognizer_ManipulationCompleted;
PointerPressedEventHandler = new PointerEventHandler(_PointerPressed);
PointerMovedEventHandler = new PointerEventHandler(_PointerMoved);
PointerReleasedEventHandler = new PointerEventHandler(_PointerReleased);
PointerCanceledEventHandler = new PointerEventHandler(_PointerCanceled);
this.AddHandler(UIElement.PointerPressedEvent, PointerPressedEventHandler, true);
this.AddHandler(UIElement.PointerMovedEvent, PointerMovedEventHandler, true);
this.AddHandler(UIElement.PointerReleasedEvent, PointerReleasedEventHandler, true);
this.AddHandler(UIElement.PointerCanceledEvent, PointerCanceledEventHandler, true);
}
private void _GestureRecognizer_ManipulationStarted(GestureRecognizer sender, ManipulationStartedEventArgs args)
{
startY = args.Position.Y;
}
private void _GestureRecognizer_ManipulationUpdated(GestureRecognizer sender, ManipulationUpdatedEventArgs args)
{
moveY += Math.Abs(args.Delta.Translation.Y);
}
private void _GestureRecognizer_ManipulationCompleted(GestureRecognizer sender, ManipulationCompletedEventArgs args)
{
if (moveY >= 50)
{
// Can be understood as sliding a distance
if (startY <= 10)
{
// head to foot
}
else if (startY >= Window.Current.Bounds.Height - 10)
{
// foot to head
}
startY = 0;
MoveY = 0;
}
}
private void _PointerPressed(object sender, PointerRoutedEventArgs e)
{
var pointer = e.GetCurrentPoint(this);
_GestureRecognizer.ProcessDownEvent(pointer);
}
private void _PointerMoved(object sender, PointerRoutedEventArgs e)
{
var pointers = e.GetIntermediatePoints(this);
_GestureRecognizer.ProcessMoveEvents(pointers);
}
private void _PointerCanceled(object sender, PointerRoutedEventArgs e)
{
var pointer = e.GetCurrentPoint(this);
_GestureRecognizer.CompleteGesture();
}
private void _PointerReleased(object sender, PointerRoutedEventArgs e)
{
var pointer = e.GetCurrentPoint(this);
_GestureRecognizer.ProcessUpEvent(pointer);
}
手指、鼠标和笔在点击屏幕时触发Pointer 相关事件。上述代码的目的是捕获Pointer事件,并传递给GestureRecognizer进行处理。
这里是document
最好的问候。