Sharing Silverlight code across platforms

Like others I started work on windows phone development before it was available by using Silverlight 3 on the desktop by simply limiting my desktop space to 800×480. One of the first apps I developed for Windows phone 7 started life this way:

My approach when the WP7 SDK became available was simply to add a wp7 project to the existing desktop solution file and begin to play with what was possible with regard to sharing code. After a few sorties down blind alleys I settled on an approach which worked well for me, enabling me to develop a single code base that was then shared between the desktop and phone projects:

In the main the bulk of the codebase remains with the desktop project with 90% of that project being made up of user controls which are shared over to the phone project using links (when adding a file to the phone project it is simply a case of navigating to the desktop project and choosing ‘link’ rather than ‘add’ within Visual Studio:

I also made a point of having separate forms (pages) for the desktop and WP7 projects that acted as simple containers for the UserControls – where all the XAML and Code actually resides. A bonus in taking this approach is that you are then in a position to take advantage of the WP7 specific features such as the ApplicationBar in the WP7 project without compromising the desktop application (where a different menuing system might be appropriate). In the event I decided to abandon the desktop ‘setup’ popup that I had developed and utilise the more appropriate menu system within the phone version of the app.

So you will notice that in the desktop app, when you click on the ‘options’ button you are presented with a popup-like experience (as on the left). Whereas in the Phone App clicking the same options button brings up the standard Windows Phone menu:

By the way game rules screen that pops up so nicely when you click on ‘help’ in the phone version of the game is simply the base MessageBox class populated with appropriate text.

The phone version of this app (which includes additional functionality to the desktop version) can be found here: