Experiments with Multi-touch: A Windows Phone Manipulation sample

I’ve just updated the project on CodePlex http://multitouch.codeplex.com to Silverlight 4 RC and added a new sample of multi-touch using Windows Phone 7 manipulation capabilities starting from this how-to available in MSDN – “How to: Handle Manipulation Events” – http://msdn.microsoft.com/en-us/library/ff426933(VS.96).aspx.

The modified sample uses an Image to illustrate the possibility to move and scale objects in the Windows Phone emulator using the “ManipulationDelta” event:

These are the steps to follow in order to enable Scale and Translation gestures in a Windows Phone application:

  • Just create a new “Windows Phone Application” from Visual Studio and then add an Image control in the MainPage.xaml using this code
<Canvas x:Name="ContentCanvas" Grid.Row="1">
    <Image Source="Images/Desert.jpg" Canvas.Left="99" Canvas.Top="100"
           x:Name="image1" Stretch="Fill" Width="300">
    </Image>
</Canvas>
  • Then add an event handler for the “ManipulationDelta” event for applying a Scale and a Translate transform using the CompositeTransform available in Silverlight 4:
public MainPage()
{
    InitializeComponent();

    SupportedOrientations = SupportedPageOrientation.Portrait | SupportedPageOrientation.Landscape;

    ManipulationDelta += image1_ManipulationDelta;
    _compositeTransform=new CompositeTransform();
    image1.RenderTransform = _compositeTransform;
}

private CompositeTransform _compositeTransform;

void image1_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
    // Scale
    if (e.DeltaManipulation.Scale.X!=0)
       _compositeTransform.ScaleX *= e.DeltaManipulation.Scale.X;
    if (e.DeltaManipulation.Scale.Y!=0)
        _compositeTransform.ScaleY *= e.DeltaManipulation.Scale.Y;

    // Translation
    _compositeTransform.TranslateX += e.DeltaManipulation.Translation.X;
    _compositeTransform.TranslateY += e.DeltaManipulation.Translation.Y;
}

The source code is available for download from CodePlex: http://multitouch.codeplex.com

Happy Silverlighting!

4 thoughts on “Experiments with Multi-touch: A Windows Phone Manipulation sample”

Leave a Reply

Your email address will not be published. Required fields are marked *