How to Pin your application in Windows Phone 7 emulator April CTP refresh

The Windows Phone 7 emulator provides Pin capability within the April CTP refresh. To pin your application perform the following:

  1. Right click on you WP7 app project and select properties.
  2. Set the Title under deployment options to your title name “PinMyApp”
  3. Set the Title under Tile options to “Pin My App” – this is the actual text shown over the tile
  4. Set the background image on the Tile using the dropdown.
  5. Deploy your application
  6. When on the Start screen Press to view your applications
  7. Hold down on your application “PinMyApp” until a context menu appears then select “pin to start”
  8.  

  9. Observe your application is now pinned

Its important to note that you can use Push notifications to change the text, count and image on your applications tile. This can be achieved by setting up a notification channel using HttpNotificationChannel and binding to the shell entry point BindToShellEntryPoint(); Once you have registered a uri will be returned and you can then send tile notifications using a WebRequest. I am currently working on a blog post for this one, so stay tuned

Nick

Upgrading your Windows Phone projects for Windows Phone 7 Developer Tools CTP April refresh

Upgrading your Windows Phone projects for Windows Phone 7 Developer Tools CTP April refresh 

Here are a bunch of breaking issues I encountered on installing Upgrading your Windows Phone projects for Windows Phone 7 Developer Tools CTP April refresh 

    For each silverlight for mobile project in your solution you get the following warning (Note: XNA framework apps will not provide a warning but will still require the changes)

  • Issue: WarningYou are using a project created by previous version of Windows Phone Developer Tools CTP. Your application may not run properly. Please edit the WMAppManifest.xml file under Properties node and insert the following <Capability> elements between
    <Capabilities></Capabilities> element as shown below. <Capabilities>
    <Capability Name="ID_CAP_NETWORKING" />
    <Capability Name="ID_CAP_LOCATION" />
    <Capability Name="ID_CAP_SENSORS" />
    <Capability Name="ID_CAP_MICROPHONE" />
    <Capability Name="ID_CAP_MEDIALIB" />
    <Capability Name="ID_CAP_GAMERSERVICES" />
    <Capability Name="ID_CAP_PHONEDIALER" />
    <Capability Name="ID_CAP_PUSH_NOTIFICATION" />
    <Capability Name="ID_CAP_WEBBROWSERCOMPONENT" />
    </Capabilities> 
  • Solution: You can find WMAppManifest.xml in the Properties folderof your project once found add the sections listed above
  • Issue: Push Notifications now require the publisher to be defined
  • Solution: This can be done within the WMAppManifest.xml in the Properties folder.  Update the Publisher on <App> E.g 
<App xmlns="" ProductID="{1ac9139d-aa7e-6c70-acf9-c6ceb69632a1}" Title="PinMyApp"
RuntimeType="SilverLight" Version="1.0.0.0" Genre="NormalApp"
Author="Nick.Harris" Description="Demo"
Publisher="www.NickHarris.net">
  • Issue: Push Notification payload has been updated
  • Solution: Update notifications as per the specification here  
  • Issue: The property ‘Visible’ does not exist on the type ‘ApplicationBar’ in the XML namespace ‘clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone.Shell’
  • Solution: this has bee renamed to IsVisible
  • Issue: ‘System.Windows.Navigation.NavigationService’ does not contain a definition for ‘TopLevelNavigationService’
  • Solution: change NavigationService.TopLevelNavigationService.Navigate(…) to NavigationService.Navigate(…)  Note: did not find this one in the breaking changes….

Release notes for other  breaking changes are here

A first look at SQL 2008 spatial data types to and from SQL Server 2008

A first look at SQL 2008 spatial data types round trip with sql 2008. I was surprised to find that the Entity Framework, LINQ to SQL and Dataset Designer do not currently support Spatial data types.  Given this was the case and wanting to have a play with the spatial data type the following is how I used the sqlgeometry spatial data type to store and retrieve a list both lines and points consisting of X,Y co-ordinates.

Lets use a bottom up approach create a table using the GEOMETRY data type:

CREATE TABLE [dbo].[MyData](
	[ID] [uniqueidentifier] NOT NULL PRIMARY KEY DEFAULT (newsequentialid()) ,
	[Data] [GEOMETRY] NULL);
GO

Create a stored procedure to insert data into the MyData table:

CREATE PROCEDURE [dbo].[InsertMyData]
            @Lines GEOMETRY
AS
	INSERT INTO [dbo].[MyData] (Data)
             VALUES (@Lines);
RETURN 0
GO

And create a stored procedure to retrieve a the data:

CREATE PROCEDURE [dbo].[GetMyData]
	@ID uniqueIdentifier
AS
	SELECT *
	FROM [dbo].[MyData]
	WHERE ID = @ID	

RETURN 0
GO

Code to insert and retrieve data:

  1. Add a reference to C:\Program Files (x86)\Microsoft SQL Server\100\SDK\Assemblies\Microsoft.SqlServer.Types.dll which contains SqlGeometryBuilder
  2.                      using System;
                         using System.Collections.Generic;
                         using System.Data;
                         using System.Data.SqlClient;
                         using System.Threading;
                         using System.Threading.Tasks;
                         using Microsoft.SqlServer.Types;
    

This is where it gets back to the !good old days without generation of your DAL. Add a method to call the InsertMyData stored procedure:

        public bool InsertMyData(Guid id, List> lines)
        {
            int result = 0;
            if (lines.Count > 0)
            {
                using (SqlConnection conn = new SqlConnection(connString))
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "InsertMyData";
                    cmd.CommandType = CommandType.StoredProcedure;

                    cmd.Parameters.Add(new SqlParameter("@ID", id));
                    cmd.Parameters.Add(SpatialDataHelper.GetGeometrySqlParameter("@Data", lines, true));
                    conn.Open();
                    result = cmd.ExecuteNonQuery();
                }
            }

            return (result > 0);
        }

Now you may be wondering what is SpatialDataHelper.GetGeometrySqlParameter. Well that too is something that needs to be implemented. Even though the SQL geometry type is a system type the type must be treated as user defined type. Take note of the sqlParam.UdtTypeName = “geometry”;

 public static class SpatialDataHelper
    {
        public static SqlParameter GetGeometrySqlParameter(string paramName, List> lines, bool makeValid)
        {
            SqlParameter sqlParam = new SqlParameter();
            sqlParam.ParameterName = paramName;
            sqlParam.UdtTypeName = "geometry";

            SqlGeometryBuilder geomBuilder = new SqlGeometryBuilder();

            // SqlGeometryBuilder.SetSrid Must be called at the beginning of each geometry sequence
            geomBuilder.SetSrid(0);
            geomBuilder.BeginGeometry(OpenGisGeometryType.GeometryCollection);

            //break out each line
            foreach (List lp in lines)
            {
                if (lp.Count > 0)
                {
                    if (lp.Count == 1) //check if its a point or a line and start a geometry for the point or line
                        geomBuilder.BeginGeometry(OpenGisGeometryType.Point);
                    else
                        geomBuilder.BeginGeometry(OpenGisGeometryType.LineString);

                    int count = 0;
                    foreach (Point p in lp) //add all points
                    {
                        if (count == 0)
                            geomBuilder.BeginFigure(p.X, p.Y);
                        else
                            geomBuilder.AddLine(p.X, p.Y);

                        count++;
                    }

                    geomBuilder.EndFigure();
                    geomBuilder.EndGeometry();
                }
            }
            geomBuilder.EndGeometry();

            SqlGeometry constructed = geomBuilder.ConstructedGeometry;
            if (makeValid)
            {
                //Note required to convert into a geometry instance with a valid Open Geospatial Consortium (OGC) type.
                // this can cause the points to shift - use with caution...
                constructed = constructed.MakeValid();
            }
            sqlParam.Value = constructed;

            return sqlParam;
        }
    }

Finally to get your data back out its necessary to call the previously defined GetMyData stored procedure and then crunch out the points into their original form.

        public List> GetMyData(Guid id)
        {
            ConcurrentBag> bag = new ConcurrentBag>();

            using (SqlConnection conn = new SqlConnection(connString))
            using (SqlCommand cmd = conn.CreateCommand())
            {
                cmd.CommandText = "GetMyData";
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add(new SqlParameter("@ID", id));
                conn.Open();

                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        startLinesID = reader.GetGuid(reader.GetOrdinal("ID"));
                        SqlGeometry gLines = (SqlGeometry)reader["Data"];
                        // using Parallel.For to speed up crunch.  Note: STGeometryN starts with 1 based index therefore start with i of 1
                        Parallel.For(1, gLines.STNumGeometries().Value + 1, delegate(int i)
                        {
                            SqlGeometry geom = gLines.STGeometryN(i);
                            List line = new List();
                            switch (geom.STGeometryType().ToString())
                            {
                                case "LineString":    //a LineString is a line within the SQLGeometry
                                    for (int j = 1; j <= geom.STNumPoints(); j++) //get all points forming the line
                                    {
                                        line.Add(new Point(geom.STPointN(j).STX.ToSqlInt32().Value, geom.STPointN(j).STY.ToSqlInt32().Value));
                                    }
                                    break;
                                case "Point": //an individual point
                                    line.Add(new Point(geom.STPointN(1).STX.ToSqlInt32().Value, geom.STPointN(1).STY.ToSqlInt32().Value)); //1 is the first index not 0 based
                                    break;
                            }
                            bag.Add(line);
                        });
                    }
                }
            }

            return bag.ToList();
        }

This seems to do the job although I would be interested to hear of better ways to do this.

New Windows Phone 7 Developer Tools CTP April refresh 2010

Download and install Windows Phone 7 Developer Tools April 2010 Refresh.

Note as part of this install I had to remove the following components in order:

  • Microsoft Visual Studio 2010  RC       
  • Microsoft Windows Phone Developer Resources         
  • Microsoft Windows Phone Developer Tools
  • Microsoft Silverlight 4.0 SDK
  • Microsoft Visual Studio 2010 Express Prerequisites x64.
  • NET Framework 4 Multi-Targeting Pack
  • Microsoft .NET Framework 4 Extended
  • Microsoft .NET Framework 4 Client Profile
  • VC 10.0 Runtime
  • Microsoft Windows Phone Emulator x64
  • Windows Phone 7 Add-in for Visual Studio 2010 – ENU
  • Microsoft XNA Game Studio 4.0

Ever had one of those days when your mouse batteries go flat in the middle of a blog post.  Then you swap out one of the batteries from the mouse and switch it with the keyboard to suck the last remaining juice from it.  Ten minutes later both are flat and you got no spares.  Lets just say its one of those days :)

Anyhow the refresh has new features (-take note of the additions to the emulator ) and breaking changes(download release notes at this link) - take note of the new publisher setting and notification templates.

Get list of installed applications

For those of you that are lazy and are required to provide a list of installed applications. Here is something i knocked together that is a good starting point Download InstalledApps exe – gets a list of installed apps. Assumes you have .NET 3.5 installed.

Its pretty bare bones but note you can pipe the output into a text file using the command prompt and typing

InstalledApps.exe > test.txt

nick

Enterprise patterns from the trenches. The Sydney Architecture User Group

The next meeting of The Sydney Architecture User Group – SAUG is on soon. Definitely worth attending, details follow:

Title: Enterprise Patterns from the trenches Presenter Omar Besiso
Date/Time: Thursday 22/04/2010 06:00 PM
Where: Grace Hotel , Kiralee or Pinaroo Function Room 77 York st Sydney,NSW. 2000

Abstract
Enterprise Applications, Enterprise Software, Enterprise Architects, Enterprise Enterprise Enterprise. What’s it all about? The Architecture User Group is proud to explain and clarify the hype. Over the next two meetings we will be having sessions to talk about what Enterprise Software is and Enterprise patterns from the trenches. Following on from this, next month’s meeting will see Paul Glavich continue with the Enterprise Architecture theme but from a web application perspective.

Presenter Bio
Omar has been a coder since he can remember! BASIC, PASCAL, COBOL, JAVA, .NET and alien languages unknown to human kind. From being a Computer Science student to Microsoft’s .NET ambassador to .NET consultant and then Enterprise Architect at Datacom, Omar has enjoyed every step of the journey one line of code at a time. Omar has worked through ASP.NET, Windows Forms, WPF, SharePoint, BizTalk, TFS and CRM projects and considers himself a Microsoft technologies veteran. He was also a speaker at TechEd events and various other MS events and user groups in Sydney, Adelaide, Brisbane, Cairo and Dubai. Omar enjoys working across multiple projects at once and his motto is “Power to the Developers”. He spends his day throwing more architectural tasks on fellow architect Paul Glavich instead of doing it himself. If you want to annoy him just say web applications are better than rich client ones.

Paul Glavich Microsoft MVP releases free e-book .NET Performance Testing and Optimization The Complete Guide

Paul Glavich Microsoft MVP sent a link to his free e-book around this morning.

“My book on performance testing, profiling and optimisation has been officially released and is available as a free download (eBook)! I think the hardcopy version will be available on amazon as a paid version.”

.NET Performance Testing and Optimization

The chapter list is:

Chapter 01: Introduction – The What and the Why
Chapter 02: Understanding Performance Targets
Chapter 03: Performance and Load Test Metrics
Chapter 04: Implementing your Test Rig
Chapter 05: Creating Performance Tests
Chapter 06: Next Steps – Profiling
Chapter 07: Performance Profiling
Chapter 08: Memory Profiling
Chapter 09: The Performance Testing Process
Chapter 10: Common Areas for Performance Improvement
Chapter 11: Load Balancing
Chapter 12: Internet Information Server (IIS)
Chapter 13: HTTP Optimization

Remember to support the hard of Paul and Chris by buying a copy of the hardcopy from Amazon

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

How to HtmlEncode user content to avoid cross site scripting attacks

Issue:  So you have put together a website that enables users to enter content that somewhere along the line renders to the screen.  Take for example www.FishingTribute.com.   

FishingTribute.com

Fishing Tribute

 Somewhere along the way a user uploads an image and injects some javascript into a label that is rendered back to the page.  The problem is labels, image text and the like are scatterd throughout the site and you dont really want to search through and HTML encode the text of every control.   

 Solution: Well one simple answer is to come up with your own control that HTML encodes the text as it renders to the page. Create a new assembly so that you can re-use it across all your sites. The following example creates a SecureLabel control.  

using System;
using System.Web;
using System.Web.UI.WebControls;

namespace Tribute.Core.Secure
{
    public class SecureLabel: Label
    {
        private bool _XSSEncode = true;

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            if(XSSEncode && !String.IsNullOrEmpty(this.Text))
            {
                this.Text = HttpUtility.HtmlEncode(this.Text);
            }

            base.Render(writer);
        }

        public bool XSSEncode
        {
            get { return _XSSEncode; }
            set { _XSSEncode = value; }
        }
    }
} 

Lets look at the code SecureLabel inherits from Label (the one we are currently using on the page). A public XSSEncode property is available to optionally change the the default value to true (i.e always html encode unless otherwise specified).    

 Override the Render method of the Label base class is and HttpUtility.HtmlEncode the text of the control if XSSEncode is true.  

But we are still not quite there yet, just as I did not want to go through my whole websites codebase and add HttpUtility.HtmlEncode(…) everywhere I dont want to go through my whole website and replace all my Label controls with SecureLabel controls. The solution is simple use the <controlls> and <tagMapping> within your web.config to register the controls and map the .net WebControl to the newly created control as per the following  

<system.web>
   <pages >
     <controls>
           …
           <add tagPrefix=“asp“ namespace=“Tribute.Core.Secure“ assembly=“Tribute.Core“/>
      </controls>
     <tagMapping>
           <add tagType=“System.Web.UI.WebControls.Label“ mappedTagType=“Tribute.Core.Secure.SecureLabel“/>
           <add tagType=“System.Web.UI.WebControls.Image” mappedTagType=“Tribute.Core.Secure.SecureImage“/>
      <tagMapping/>
  <pages/>
<system.web/>

There you have it – a low effort no fuss solution :)

A heads up from Phil on Literals usage – use the mode=”Encode” property

ABN Validation in C#

Helper Class for ABN Validation follows as per ATO rules found

        public static class ABNValidationHelper
        {
            //http://www.ato.gov.au/businesses/content.asp?doc=/content/13187.htm&pc=001/003/021/002/001&mnu=610&mfp=001/003&st=&cy=1
            //1. Subtract 1 from the first (left) digit to give a new eleven digit number
            //2. Multiply each of the digits in this new number by its weighting factor
            //3. Sum the resulting 11 products
            //4. Divide the total by 89, noting the remainder
            //5. If the remainder is zero the number is valid
            public static bool ValidateABN(string abn)
            {
                bool isValid = true; int[] weight = { 10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 }; int weightedSum = 0;
                //0. ABN must be 11 digits long
                if (isValid &= (!string.IsNullOrEmpty(abn) && Regex.IsMatch(abn, @"^\d{11}$")))
                {
                    //Rules: 1,2,3
                    for (int i = 0; i < weight.Length; i++) { weightedSum += (int.Parse(abn[i].ToString()) - ((i == 0) ? 1 : 0)) * weight[i]; }
                    //Rules: 4,5
                    isValid &= ((weightedSum % 89) == 0);
                } return isValid;
            }
        }