Understanding Touch Support in Windows Phone

Introduction

Like other prominent touch aware phone platforms, Microsoft’s Windows Phone "Mango" offers a rich framework for capturing touch and multi-touch activity on Windows Phone devices.

In this article, we will learn the underlying APIs that developers need to work with to leverage the true power of the Windows Phone platform.

Touch Support

The touch support in Windows Phone is exposed to Windows Phone developers through the FrameReported event in the Touch class, which resides in System.Windows.Input namespace. The Touch class resides in System.Windows.dll, which is one of the default libraries included in all Windows Phone applications. The FrameReported event gets raised for any touch activity across the currently running application.

Each frame raised represents a series of touch messages/points, which can be retrieved by calling GetTouchPoints API or calling the member methods on the TouchFrameEventArgs instance in the FrameReported event.

The FrameReported event is generated in the following cases:

  1. When a finger makes screen contact
  2. When a finger slides on a screen
  3. When a finger is released from the screen

When a series of touches are made, you can get the first touch points by calling GetPrimaryTouchPoint on the TouchFrameEventArgs instance raised in the FrameReported event.

Hands-On

Start Visual Studio and create a new Windows Phone project titled "TouchDemo".

Create a new project in Visual Studio
Create a new project in Visual Studio

When prompted for Windows Phone OS version, select "Windows Phone OS 7.1".

Select Windows Phone OS 7.1
Select Windows Phone OS 7.1

Let's add two TextBlocks, one with text "TouchPoint" and the other blank. The second TextBlock will indicate the dimension of the touch event.

Now add an event handler for processing any FrameReported events. We’ll call our emulator "MyAppFrameReported". In this event handler, we will get the Primary touch point and if the touch action is down or move, we will show it in the textblock we created above.

void MyAppFrameReported(object sender, TouchFrameEventArgs e)
        {
            TouchPoint tp = e.GetPrimaryTouchPoint(this);
            while (tp.Action != TouchAction.Up)
            {
                textBlockTouchPoint.Text = tp.Position.X.ToString() + "," + tp.Position.X.ToString();
            }
        }

Next, we need to wire our event handler. We will add a hook to the event when we navigate to the page.

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            Touch.FrameReported += MyAppFrameReported;
        }

We also want to make sure that we don’t process the eventhandler multiple times. To do that, when we navigate away from the page, we want to de-register the event handler. We do that in the OnNavigatedFrom eventhandler.

protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);
            Touch.FrameReported -= MyAppFrameReported;
        }

Now, our application is ready to handle touch events and show the location of the touches. Note that the Visual Studio Emulator does not raise the FrameReported event since it required touch sensitive hardware. To test this application, you need to deploy the application to a phone to test it.

When you run the application, you will notice that on every touch, the TouchPoint location is updated. That is because, for every FrameReported event fired, we look at the position and report it.

Summary

I hope you have found the information useful. Please be very careful to detach FrameReported handlers as soon as possible. Unintended consequences can occur if an application navigates away from the page.

Download Touch Demo

About the author

Vipul Patel is a Program Manager currently working at Amazon Corporation. He has formerly worked at Microsoft in the Lync team and in the .NET team (in the Base Class libraries and the Debugging and Profiling team). He can be reached at vipul_d_patel@hotmail.com



Related Articles

Downloads

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds