Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.
Capturing Gestures with TouchPanel in Windows Phone
This article demonstrates how to capture gesture events.
This article shows how to capture a gesture in our Silverlight application using the XNA assembly Microsoft.Xna.Framework.Input.Touch.
The example app first declares what type of gesture we want to capture (just simple user touch input), and once we capture that particular gesture we perform some task. This task could be anything - in this case we just display what type of gesture we have captured.
First create a project with Windows Phone Application Template. Once the project is being created we add Microsoft.Xna.Framework.Input.Touch assembly reference in our project.
In XNA the touch surface is involved with the TouchPanel. With the help of this TouchPanel we can access the Gesture API and can enable the type of gesture we are interested in. In constructor we enable the type of gesture we want in this application.
TouchPanel.EnabledGestures = GestureType.Hold | GestureType.Tap | GestureType.DoubleTap |
GestureType.Flick | GestureType.Pinch|GestureType.DragComplete|GestureType.FreeDrag|
EnabledGestures gets or sets the gestures that are enabled for the touch panel with a single or a combination of gestures that are listed in the GestureType enumeration. It needs the Namespace: Microsoft.Xna.Framework.Input.Touch. Till now we have enabled the gesture that we are looking for, now we need to recognize them. As in this example we are planing to capture the gesture that happens anywhere in the screen, so we call ManipulationCompleted event with LayoutRoot control to occupy the complete screen.
LayoutRoot.ManipulationCompleted += new EventHandler<ManipulationCompletedEventArgs>(LayoutRoot_ManipulationCompleted);
In the event handler LayoutRoot_ManipulationCompleted() we first check is gesture available or not .
Then we read gesture on the touch panel and compare it in a switch() statement. Once the gesture matches we display the name of the gesture in the screen.
void LayoutRoot_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
GestureSample gesture = TouchPanel.ReadGesture();
gestureText.Text = "FreeDrag";
gestureText.Text = "HorizontalDrag";
gestureText.Text = "Pinch";
gestureText.Text = "VerticalDrag";
gestureText.Text = "Tap";
gestureText.Text = "Double Tap";
gestureText.Text = "Hold";
gestureText.Text = "Flick";
Here GestureSample is the data that we get from a multitouch gesture after a time period.
The full source code of the example is available here: File:GesturesCapturing.zip