Tip of the day: Processing touch input on Windows Phone 7

DZone 's Guide to

Tip of the day: Processing touch input on Windows Phone 7

· Mobile Zone ·
Free Resource

Let's think about the ways you used to control touch in your Silverlight-based Windows Phone 7 applications before. Chances are, you were trying to use the regular mouse capture method by handling mouse events - just like you did in any other Silverlight application. But as you probably noticed, it wasn't quite working out.

Now let's look at the documentation for Windows Phone 7, specifically at the Performance Considerations in Applications for Windows Phone 7 article:

Manipulation events are the recommended way to handle user input. Unless you have a specific need, you should avoid using mouse events in your Silverlight applications for Windows Phone for performance and hardware compatibility reasons. Instead, you should use manipulation events.

So it becomes obvious that using mouse capture isn't the right thing to do. What's there instead? You can always handle the Touch.FrameReported event. You could be already familiar with this event if you worked with Windows 7 touch handling in Silverlight, but if not, it's not that complicated.

You can simply add handling to it in the page constructor:

Touch.FrameReported += new TouchFrameEventHandler(Touch_FrameReported);

Once referenced, the event handler itself exposes an instance of TouchFrameEventArgs:

void Touch_FrameReported(object sender, TouchFrameEventArgs e)


And here is where you can do all kinds of cool stuff when the user touches the screen. First of all, you can access GetTouchPoints that will let you get the X and Y coordinates of the touch point relative to a UIElement instance.

For example:


In this specific instance, you will get the first instance of a position point and will display the X coordinate for it. You might ask - why am I passing an index here and the answer is simple - this event handles multitouch, therefore there could be multiple simultaneous touch points present.

GetTouchPoints returns an instance of TouchPointCollection, that implements PresentationFrameworkCollection:

public sealed class TouchPointCollection : PresentationFrameworkCollection<TouchPoint>
// Methods
internal TouchPointCollection()

This means that if you want to (or need to), you can run LINQ queries against this TouchPoint set.

What I did there would be the same if I would call this:


This will get only one point - the one that is currently touched (and touched first). You are also able to determine the current action - whether the user pushed down, moved the finger across the surface or lifted it:


So when I touch the screen, the result I will see will be Down. When I move, I will be getting Move and when I remove the finger from the touch surface, I will get Up.

The same way you can get the size of the rectangle that represents the touched area:


Interestingly enough, you can even track down the time difference between touch events. For example, like this:

int touchTime = 0;

void Touch_FrameReported(object sender, TouchFrameEventArgs e)
if (e.GetPrimaryTouchPoint(this).Action == TouchAction.Down)
Debug.WriteLine(e.Timestamp - touchTime);
touchTime = e.Timestamp;

Remember though, that according to the same article (Performance Considerations in Applications for Windows Phone) you should not handle pinch and stretch events through FrameReported but rather use manipulation events.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}