Silverlight for Mobile on Windows Phone 7 InkPresenter fun

After installing the Windows Phone Developer Tools this is a simple test for a bit of fun using Silverlight for Mobile for the first time to capturing user strokes using the InkPresenter.

  1. In Visual Studio 2010 Press File –> New Project –> Silverlight for Windows Phone –> Windows Phone Application
  2. Creating a Silverlight for Mobile Application

    Creating a Silverlight for Mobile Application

  3. Add an InkPresenter and two buttons Undo and Redo to the to the content Grid
  4.  

          <!--ContentGrid is empty. Place new content here-->
            <Grid x:Name="ContentGrid" Grid.Row="1">
                <InkPresenter  Name="inkTest" Background="White" Margin="0,0,0,62" />
                <Button Name="btnUndo" Content="Undo" Grid.Row="1" Height="72" HorizontalAlignment="Left" Margin="44,584,0,0" VerticalAlignment="Top" Width="160" Click="btnUndo_Click" Background="#FF933A3A" />
                <Button Name="btnRedo" Content="Redo" Height="72" HorizontalAlignment="Left" Margin="272,584,0,0" VerticalAlignment="Top" Width="160" Grid.Row="1" Click="btnRedo_Click" Background="#FF933A3A" />
            </Grid>
  5. On the InkPresenter, named inkTest in this example, Add Event handlers for MouseMove, MouseLeftButtonDown, MouseLeftButtonUp to capture the movement from the user
  6.             inkTest.MouseMove += new MouseEventHandler(inkTest_MouseMove);
                inkTest.MouseLeftButtonDown += new MouseButtonEventHandler(inkTest_MouseLeftButtonDown);
                inkTest.MouseLeftButtonUp += new MouseButtonEventHandler(inkTest_MouseLeftButtonUp);
  7. To capture the mouse movements and turn them into Strokes on the InkPresenter the corresponding EventHandlers and member variables are as follows
  8. private Stroke _currentStroke;
    
    private void inkTest_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        _currentStroke = null;
    }
    
    private void inkTest_MouseMove(object sender, MouseEventArgs e)
    {
        if (_currentStroke != null)
            _currentStroke.StylusPoints.Add(GetStylusPoint(e.GetPosition(inkTest)));
    }
    
    private void inkTest_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        inkTest.CaptureMouse();
        _currentStroke = new Stroke();
        _currentStroke.StylusPoints.Add(GetStylusPoint(e.GetPosition(inkTest)));
        _currentStroke.DrawingAttributes.Color = Colors.Blue;
        inkTest.Strokes.Add(_currentStroke);
    }
    
    private StylusPoint GetStylusPoint(Point position)
    {
        return new StylusPoint(position.X, position.Y);
    }
  9. And since its difficult to draw with a mouse I want to be able to undo and redo some of my Strokes I use a simple Stack so implement the following EventHandlers for the Undo and Redo buttons
  10.         private Stack _removedStrokes = new Stack();
    
            private void btnUndo_Click(object sender, RoutedEventArgs e)
            {
                if (inkTest.Strokes != null && inkTest.Strokes.Count > 0)
                {
                    _removedStrokes.Push(inkTest.Strokes.Last());
                    inkTest.Strokes.RemoveAt(inkTest.Strokes.Count - 1);
                }
            }
    
            private void btnRedo_Click(object sender, RoutedEventArgs e)
            {
                if (_removedStrokes != null && _removedStrokes.Count > 0)
                {
                    inkTest.Strokes.Add(_removedStrokes.Pop());
                }
            }
  11. And the final result is :)
  12. InkPresenter on Windows Phone 7

    InkPresenter on Windows Phone 7

This demonstrated a simple application that was faster to code then to blog about and that even with an undo button I am still hopeless at drawing :) . Try implementing the same functionality in the .NET Compact Framework :)

Next posts will look at

  • Reworking this application to use MVVM
  • Using the Microsoft Notification Service

17 thoughts on “Silverlight for Mobile on Windows Phone 7 InkPresenter fun

  1. Pingback: Tweets that mention Silverlight for Mobile on Windows Phone 7 InkPresenter fun - Nick Harris .NET -- Topsy.com

  2. Pingback: Adding an Application Bar to your Windows Phone 7 application - Nick Harris .NET

  3. Pingback: Implementing MVVM for the first time on Windows Phone 7 using Silverlight for Mobile - Nick Harris .NET

  4. I get some errors that I can’t move on with, with this code:

    Using the generic type ‘System.Collections.Generic.Stack’ requires 1 type arguments

    The type or namespace name ‘Stroke’ could not be found (are you missing a using directive or an assembly reference?)

    What should I do?

  5. Pingback: Lista de recursos para desarrolladores de Windows Phone 7 - Windows Phone 7 Puerto Rico

  6. Hi Neodudeman,

    Ensure you have a reference to C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\Silverlight\v4.0\Profile\WindowsPhone\system.dll for Stack and a reference to
    C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\Silverlight\v4.0\Profile\WindowsPhone\System.Windows.dll for Stroke and add the following using directives at the top of your codebehind

    using System.Windows.Ink;
    using System.Collections.Generic;

  7. Hi Sergey,

    Firstly – Cool an MVP commenting on my blog :) … agree yes its based on the .NET CF. I guess I was getting at what SL for Mobile brings to developers – If i had tried to implement the same functionality as InkPresenter in a .NET CF win forms application I believe it would have taken a lot longer :)

    Nick

  8. Pingback: Links to sample code for the Windows Phone 7 « Bruce Barrera

  9. Hey Neodudeman,

    For your 1st error, all you’ve gotta do is add the type as Stroke for the stack when you declare the removedStrokes stack, like this :

    private Stack _removedStrokes = new Stack ();

    The 2nd of course will be resolved by adding the System.Windows.Ink reference

  10. Pingback: A Simple Paint App on Windows Phone 7 using InkPresenter « tried…. It worked….. :)

Leave a Reply

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

*

* Copy this password:

* Type or paste password here:

5,253 Spam Comments Blocked so far by Spam Free Wordpress

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>