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

Future post content – Windows Phone 7 development

Given that I now have the Windows Phone  Developer Tools up and running posts wil start focusing more on Windows Phone 7 development.  I have a number of posts in progress if your interested the following is a list of larger posts currently in progress

a.  Merge replication logging inside out using SQL Server, SQL Server CE and web synchronisation
b. WS-AT over WCF configuration and implementation – covers firewall, registry, certs and WS-AT command line and Windows SDK GUI.
c. Streamline WS-AT configuration using a UI without need for windows SDK UI snapin- a little something I am working on
d. Visual Studio Project Templates
e. Scheduled Windows Service with plugable architecture for scheduling tasks

At this point in time I will probably abandon the above posts for quite some time while I focus on Windows Phone dev.  If any of the above are of particular interest to you please add a comment detailing the prioritised order of the post(s) you would like to see.

What a day – dev therapy with tonights downloads + installs

When you have a bad day there is nothing better then some dev therapy = tonights downloads + installs;

  1. .NET Framework 4 full RC
  2. Visual Studio 2010 Ultimate RC
  3. Visual Studio 2010 remote debugger RC
  4. Windows Phone Developer Tools CTP includes
    • Windows Phone Emulator CTP
    • Silverlight for Windows Phone CTP
    • XNA 4.0 Game Studio CTP
  5. Microsoft Expression Blend 4 Beta
  6. Visual Studio 2010 and .NET Framework 4 Training Kit

How To Configure Storage Card for the Windows Mobile emulator

Issue: You require large files, such as a SQL Server CE database, to be used on the emulator but dont have enough space.

Solution: You can resolve this by setting up a storage card for the emulator that maps to a local folder on your PC.

Steps to configure PC:

  1. Create a folder on your local hard drive such as c:\emulator\Storage Card
    This will be configured as the storage card for your emulator in the next step
Windows Mobile Emulator Storage Card Configuration

Windows Mobile Emulator Storage Card Configuration

Steps to configure the Emulator:

  1. In Device emulator select File menu –> Configure
  2. Select the General tab
  3. Set the shared folder to c:\emulator\Storage Card in the shared textbox
  4. press ok
  5. Windows Mobile Emulator Storage Card Configuration

    Windows Mobile Emulator Storage Card Configuration

  6. On the enykatir go to File –> Reset –> Hard
  7. Once the emulator boots go to Start –> Programs –> File Explorer and select Storage card
Windows Mobile Emulator Storage Card Configuration

Windows Mobile Emulator Storage Card Configuration

Useful NativeError Range and Description links: SQL Server Compact 3.5 Service Pack 1 and SQL Server 2005 Compact Edition

Essential links for figuring out why your SQL Server Compact Edition merge replication is failing. 

SQL Server Compact Edition 3.5 Service Pack 1 NativeError List:

Category Range
Engine Errors 25000-25499
Replication Transport Errors 28000-28499
Client Agent Errors 28500-28999
Server Agent Errors 29000-29499
Message Protocol Errors 29500-29999
Query Processor errors 25500-26499
OLEDB Errors 0x80040E00L-0x00040EDDL

SQL Server Mobile 2005 Compact Edition NativeError List:

Category Range
Engine Errors 25000-27999
Replication Transport Errors 28000-28499
Client Agent Errors 28500-28999
Server Agent Errors 29000-29499
Message Protocol Errors 29500-29999
Query Processor errors 25500-26499
OLEDB Errors 0x80040E00L-0x00040EDDL

Application crash when using SQL Server Compact Edition running from storage card in the Symbol MC70

So you have got your new MC70, slapped in a storage card so that you can run your SQL Server CE database from there and found that you application keeps crashing everytime the device resumes from standby?

The issue is the default driver selection for the SD Card causes the connection to break, perform the following to resolve the issue:

  1. Press Start –> Setings –> System Tab –> SDSwitch Icon
  2. Press SDMMC Mode radio button
  3. Press No Remove On Resume checkbox
  4. Press Ok
  5. Perform a warm boot of the device as advised by dialog
 

MC70 SD Switch for SQL Server Compact=

MC70 SD Switch for SQL Server Compact Edition