C# 在 WinRT 应用程序中处理向上滑动、向下滑动、向左滑动和向右滑动手势
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12648318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Handle Swipe Up, Swipe Down, Swipe Left & Swipe Right Gestures in a WinRT app
提问by Elmo
I have the following code:
我有以下代码:
public MainPage()
{
this.InitializeComponent();
this.ManipulationStarting += MainPage_ManipulationStarting;
this.ManipulationStarted += MainPage_ManipulationStarted;
this.ManipulationInertiaStarting += MainPage_ManipulationInertiaStarting;
this.ManipulationDelta += MainPage_ManipulationDelta;
this.ManipulationCompleted += MainPage_ManipulationCompleted;
}
void MainPage_ManipulationStarting(object sender, ManipulationStartingRoutedEventArgs e)
{
Debug.WriteLine("MainPage_ManipulationStarting");
}
void MainPage_ManipulationStarted(object sender, ManipulationStartedRoutedEventArgs e)
{
Debug.WriteLine("MainPage_ManipulationStarted");
}
void MainPage_ManipulationInertiaStarting(object sender, ManipulationInertiaStartingRoutedEventArgs e)
{
Debug.WriteLine("MainPage_ManipulationInertiaStarting");
}
void MainPage_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
Debug.WriteLine("MainPage_ManipulationDelta");
}
void MainPage_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
{
Debug.WriteLine("MainPage_ManipulationCompleted");
}
But I have no idea on how to use the Manipulation events. Can you anyone describe how to handle the gestures swipe up, down, left and right?
但我不知道如何使用 Manipulation 事件。你能描述一下如何处理向上、向下、向左和向右滑动的手势吗?
采纳答案by Jawahar
Manipulation events provide you the translation values. Manipulation Delta will fire continuously until your manipulation completed along with inertia. In this event check whether the move is inertial, (a normal move shouldn't be considered as swipe) and detect the difference between initial and current position.
操作事件为您提供翻译值。Manipulation Delta 将持续触发,直到您的操作与惯性一起完成。在这种情况下,检查移动是否是惯性移动(正常移动不应被视为滑动)并检测初始位置和当前位置之间的差异。
Once it reached the threshold, fire the swipe up/down/left/right event. And stop the manipulation immediately to avoid firing the same event again and again.
一旦达到阈值,触发向上/向下/向左/向右滑动事件。并立即停止操作,以避免一次又一次地触发相同的事件。
Following code will help you,
以下代码将帮助您,
private Point initialpoint;
private void Grid_ManipulationStarted_1(object sender, ManipulationStartedRoutedEventArgs e)
{
initialpoint = e.Position;
}
private void Grid_ManipulationDelta_1(object sender, ManipulationDeltaRoutedEventArgs e)
{
if (e.IsInertial)
{
Point currentpoint = e.Position;
if (currentpoint.X - initialpoint.X >= 500)//500 is the threshold value, where you want to trigger the swipe right event
{
System.Diagnostics.Debug.WriteLine("Swipe Right");
e.Complete();
}
}
}
回答by drcoderz
I tried the answer by XAML lover, but it wasn't that accurate for me (IsIntertial always came back false for me). I implemented something different (I replied to a previous post of a related topic here Handling Swipe Guesture in Windows 8 Grid) for anyone who wanted to try something different.
我尝试了 XAML 爱好者的答案,但对我来说并不是那么准确(IsIntertial 对我来说总是错误的)。我为任何想要尝试不同的东西的人实现了一些不同的东西(我回复了一个相关主题的前一篇文章在 Windows 8 网格中处理滑动客人)。
回答by maxim pg
Take a look at GestureRecognizer.CrossSliding event. There is also EdgeGestureclass, and samples: EdgeGesture sample, gestures sample.
看看GestureRecognizer.CrossSliding 事件。还有EdgeGesture类和示例:EdgeGesture 示例、手势示例。

