2012-09-04 149 views
21

任何人都可以解釋如何在WinRT應用程序中檢測簡單的觸摸手勢嗎?我嘗試使用GestureRecognizer類,但它沒有工作:檢測簡單的觸摸手勢

public MainPage() 
    { 
     this.InitializeComponent(); 
     Windows.UI.Input.GestureRecognizer gr = new Windows.UI.Input.GestureRecognizer(); 
     gr.CrossSliding += gr_CrossSliding; 
     gr.Dragging += gr_Dragging; 
     gr.Holding += gr_Holding; 
     gr.ManipulationCompleted += gr_ManipulationCompleted; 
     gr.ManipulationInertiaStarting += gr_ManipulationInertiaStarting; 
     gr.ManipulationStarted += gr_ManipulationStarted; 
     gr.ManipulationUpdated += gr_ManipulationUpdated; 
     gr.RightTapped += gr_RightTapped; 
     gr.Tapped += gr_Tapped; 
     gr.GestureSettings = Windows.UI.Input.GestureSettings.ManipulationRotate | Windows.UI.Input.GestureSettings.ManipulationTranslateX | Windows.UI.Input.GestureSettings.ManipulationTranslateY | 
     Windows.UI.Input.GestureSettings.ManipulationScale | Windows.UI.Input.GestureSettings.ManipulationRotateInertia | Windows.UI.Input.GestureSettings.ManipulationScaleInertia | 
     Windows.UI.Input.GestureSettings.ManipulationTranslateInertia | Windows.UI.Input.GestureSettings.Tap; 

    } 

    void gr_Tapped(Windows.UI.Input.GestureRecognizer sender, Windows.UI.Input.TappedEventArgs args) 
    { 
     Debug.WriteLine("gr_Tapped"); 
    } 
    void gr_RightTapped(Windows.UI.Input.GestureRecognizer sender, Windows.UI.Input.RightTappedEventArgs args) 
    { 
     Debug.WriteLine("gr_RightTapped"); 
    } 
    void gr_Holding(Windows.UI.Input.GestureRecognizer sender, Windows.UI.Input.HoldingEventArgs args) 
    { 
     Debug.WriteLine("gr_Holding"); 
    } 
    void gr_Dragging(Windows.UI.Input.GestureRecognizer sender, Windows.UI.Input.DraggingEventArgs args) 
    { 
     Debug.WriteLine("gr_Dragging"); 
    } 
    void gr_CrossSliding(Windows.UI.Input.GestureRecognizer sender, Windows.UI.Input.CrossSlidingEventArgs args) 
    { 
     Debug.WriteLine("gr_CrossSliding"); 
    } 
    void gr_ManipulationUpdated(Windows.UI.Input.GestureRecognizer sender, Windows.UI.Input.ManipulationUpdatedEventArgs args) 
    { 
     Debug.WriteLine("gr_ManipulationUpdated"); 
    } 
    void gr_ManipulationStarted(Windows.UI.Input.GestureRecognizer sender, Windows.UI.Input.ManipulationStartedEventArgs args) 
    { 
     Debug.WriteLine("gr_ManipulationStarted"); 
    } 
    void gr_ManipulationCompleted(Windows.UI.Input.GestureRecognizer sender, Windows.UI.Input.ManipulationCompletedEventArgs args) 
    { 
     Debug.WriteLine("gr_ManipulationCompleted"); 
    } 
    void gr_ManipulationInertiaStarting(Windows.UI.Input.GestureRecognizer sender, Windows.UI.Input.ManipulationInertiaStartingEventArgs args) 
    { 
     Debug.WriteLine("gr_ManipulationInertiaStarting"); 
    } 
+0

我不明白,你已經添加了一個處理程序輕敲的事件。 – test

回答

37

如果你會發現MainPage類具有可以在不創建一個單獨的GestureRecognizer使用自己的操作事件。您可以通過將this.ManipulationMode設置爲ManipulationModes.All來啓用它。這將允許您在MainPages Tapped,RightTapped,ManipulationStarting,ManipulationStarted,ManipulationDeltaManipulationCompleted事件中看到響應。

至於獲得GestureRecongnizer根據本Blog,這MSDN Forum Posting你將需要處理的MainPage的PointerMovedPointerReleasedPointerPressed事件,像這樣的工作。

Windows.UI.Input.GestureRecognizer gr = new Windows.UI.Input.GestureRecognizer(); 

public MainPage() 
{ 
    this.InitializeComponent(); 
    this.PointerPressed += MainPage_PointerPressed; 
    this.PointerMoved += MainPage_PointerMoved; 
    this.PointerReleased += MainPage_PointerReleased; 
    gr.CrossSliding += gr_CrossSliding;  
    gr.Dragging += gr_Dragging;  
    gr.Holding += gr_Holding;  
    gr.ManipulationCompleted += gr_ManipulationCompleted;  
    gr.ManipulationInertiaStarting += gr_ManipulationInertiaStarting;  
    gr.ManipulationStarted += gr_ManipulationStarted;  
    gr.ManipulationUpdated += gr_ManipulationUpdated;  
    gr.RightTapped += gr_RightTapped;  
    gr.Tapped += gr_Tapped;  
    gr.GestureSettings = Windows.UI.Input.GestureSettings.ManipulationRotate | Windows.UI.Input.GestureSettings.ManipulationTranslateX | Windows.UI.Input.GestureSettings.ManipulationTranslateY |  
    Windows.UI.Input.GestureSettings.ManipulationScale | Windows.UI.Input.GestureSettings.ManipulationRotateInertia | Windows.UI.Input.GestureSettings.ManipulationScaleInertia |  
    Windows.UI.Input.GestureSettings.ManipulationTranslateInertia | Windows.UI.Input.GestureSettings.Tap; 
} 

void MainPage_PointerReleased(object sender, PointerRoutedEventArgs e) 
{ 
    var ps = e.GetIntermediatePoints(null); 
    if (ps != null && ps.Count > 0) 
    { 
     gr.ProcessUpEvent(ps[0]); 
     e.Handled = true; 
     gr.CompleteGesture(); 
    } 
} 

void MainPage_PointerMoved(object sender, PointerRoutedEventArgs e) 
{ 
    gr.ProcessMoveEvents(e.GetIntermediatePoints(null)); 
    e.Handled = true; 
} 

void MainPage_PointerPressed(object sender, PointerRoutedEventArgs e) 
{ 
    var ps = e.GetIntermediatePoints(null); 
    if (ps != null && ps.Count > 0) 
    { 
     gr.ProcessDownEvent(ps[0]); 
     e.Handled = true; 
    } 
} 

根據你需要將它添加到您的GestureRecongnizer和設置CrossSlideThresholds和方向,使CrossSlide事件的Documentation。 從上次鏈接:

必須在GestureSettings屬性中設置CrossSlide以支持CrossSliding。 默認情況下禁用交叉滑動距離閾值。使用CrossSlideThresholds設置這些值。

例如:

Windows.UI.Input.CrossSlideThresholds cst = new Windows.UI.Input.CrossSlideThresholds(); 
cst.SelectionStart = 2; 
cst.SpeedBumpStart = 3; 
cst.SpeedBumpEnd = 4; 
cst.RearrangeStart = 5; 
gr.CrossSlideHorizontally = true; 
gr.CrossSlideThresholds = cst; 
gr.CrossSliding += gr_CrossSliding; 

,並確保它被添加到您的GestureSettings

gr.GestureSettings = Windows.UI.Input.GestureSettings.ManipulationRotate | Windows.UI.Input.GestureSettings.ManipulationTranslateX | 
        Windows.UI.Input.GestureSettings.ManipulationScale | Windows.UI.Input.GestureSettings.ManipulationRotateInertia | 
        Windows.UI.Input.GestureSettings.ManipulationScaleInertia | Windows.UI.Input.GestureSettings.ManipulationTranslateInertia | 
        Windows.UI.Input.GestureSettings.Tap | Windows.UI.Input.GestureSettings.CrossSlide;