Wednesday, February 08, 2012

The boy in me chuckles at this post's title... "Mango Sample: Give your app the Finger!"

Jerry Nixon @work - Mango Sample: Give your app the Finger!


My colleague Developer Evangelist, Sam Stokes, in California has written a compelling article on dragging items in Windows Phone.

But I have a problem with Sam’s approach

Maybe it’s because I have a problem with Sam – but probably not; he’s actually one of my top 90,000 people @ Microsoft. So, let’s talk about moving things in XAML!

Sam moves objects around by incrementing and decrementing an item’s top/left dependency property inside a Canvas.


Is there an alternative?

Yes. It’s TranslateTransform – a Silverlight class specifically designed to accommodate the task of moving objects from one position to another. (TranslateTransform is also part of WPF). And get this, even in the documentation dips into this discussion:

MSDN: The local 0,0 for an object can be offset on a Canvas using Canvas.Left and Canvas.Top, but this does not count as a transform; the object retains its own local 0,0 in this case for transform purposes.

A TranslateTransform is easy to use. A TranslateTransform has an X and Y property which correlate to the relative position to the element’s original layout location. As you would expect, negative numbers move it in a reverse direction


In the code above, I handle the ManipulationDelta event to track the touch gestures by the user. In the handler, I increment and decrement the X and Y values of the TranslateTransform – just like Sam. And just like Sam, my object tracks (or moves) with the user’s finger. But unlike Sam, my approach is hardware accelerated and a common, repeatable pattern.


Admittedly, I am sort of picking on my colleague to make a point. The reality is, Sam’s approach works. In fact, it is a very common approach to moving elements in a XAML environment, but there are downsides – do those downsides really matter? Maybe not enough for you to rewrite your application, but then again maybe so.

You’re the developer, you need to make the final call. Now you have a little more information. Informed decisions are usually better decisions. But I can’t tell you what is right for your application. What I can tell you is that TranslateTransform is easy to use, powerful, high performing, and a consistent approach. Other than that, it’s up to you.

Special acknowledgement to Sam Stokes for starting a great discussion


Not only was the title attention getting, but the content was interesting too! (Gosh, imagine that! lol)

No comments: