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.
- In Visual Studio 2010 Press File –> New Project –> Silverlight for Windows Phone –> Windows Phone Application
- Add an InkPresenter and two buttons Undo and Redo to the to the content Grid
- On the InkPresenter, named inkTest in this example, Add Event handlers for MouseMove, MouseLeftButtonDown, MouseLeftButtonUp to capture the movement from the user
- To capture the mouse movements and turn them into Strokes on the InkPresenter the corresponding EventHandlers and member variables are as follows
- 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
- And the final result is
<!--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>
inkTest.MouseMove += new MouseEventHandler(inkTest_MouseMove);
inkTest.MouseLeftButtonDown += new MouseButtonEventHandler(inkTest_MouseLeftButtonDown);
inkTest.MouseLeftButtonUp += new MouseButtonEventHandler(inkTest_MouseLeftButtonUp);
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);
}
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());
}
}
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


Hey homeslice. Take a look at this blog, http://sachabarber.net/ it’s got a lot of info for MVVM for WPF, might be easily applied to Silverlight, not sure tho since I haven’t done any SL yet
how cool is that. Thanks
Cool thanks Phil, will give it a look
Glad you liked it Sal, Enjoy
Pingback: Tweets that mention Silverlight for Mobile on Windows Phone 7 InkPresenter fun - Nick Harris .NET -- Topsy.com
Pingback: Adding an Application Bar to your Windows Phone 7 application - Nick Harris .NET
Pingback: Implementing MVVM for the first time on Windows Phone 7 using Silverlight for Mobile - Nick Harris .NET
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?
Pingback: Lista de recursos para desarrolladores de Windows Phone 7 - Windows Phone 7 Puerto Rico
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;
>> Try implementing the same functionality in the .NET Compact Framework
WP7 based on .NET Compact Framework too
))
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
Great information. Thanks for the post.
Pingback: Links to sample code for the Windows Phone 7 « Bruce Barrera
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
I meant use “Stack ” instead of just “Stack” in the declaration of the stack.
Pingback: A Simple Paint App on Windows Phone 7 using InkPresenter « tried…. It worked….. :)