2012-09-28 32 views
15

我有以下代码:手柄向上轻扫,可向下滑动,向左滑动和向右滑动,即可手势在WinRT的应用

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"); 
} 

但我对如何使用操作事件不知道。你可以描述如何处理手势向上,向下,向左或向右滑动吗?

+1

我没有使用过WinRT手势,但是快速浏览一下MSDN,我会打赌你可以在'Delta'(或其他)事件期间访问参数的各种属性。例如'ManipulationDeltaRoutedEventArgs.Velocities'具有一组关于来自用户的方向/角度/标度输入的数据。我不能说这是你应该看到的“那个”,但也许它会给你一个开始。 –

回答

21

操作事件为您提供翻译值。操纵Delta将持续发射,直到你的操控与惯性一起完成。在这种情况下,检查移动是否是惯性的(正常移动不应被视为滑动)并检测初始位置和当前位置之间的差异。

一旦达到阈值,就会触发滑动上/下/左/右事件。立即停止操作,以避免一次又一次触发相同的事件。

下面的代码将帮助你,

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(); 
      } 
     } 
    } 
+3

不需要多个活动。只需使用'e.Cumulative.Translation.X'。 –

4

我试图通过XAML情人的答案,但它不是为我准确的(IsIntertial总是回来假对我来说)。对于想要尝试不同的人,我实施了一些不同的东西(我回复了以前相关主题的帖子Handling Swipe Guesture in Windows 8 Grid)。