Experiments with Silverlight

So how easy is it to take a WPF project and run it as Silverlight instead? As with so many things – it depends. OK, so the code I converted was fairly trivial and completely unrepresentative but it was interesting to see just how easy it can be, and how frustrating it can be.

How things change

Basic classes

Exception – in .Net – Serializable, in Silverlight – not so much.

So simple exception subclasses need conditional code. I wasn’t expecting problems with code quite so trivial.

My approach was basically try and compile it and if it broke flip between the .Net and Silverlight class definitions on MSDN until I saw what was different. There must be a better way!

#if !SILVERLIGHT
	[Serializable]
#endif
public class GameException : Exception
{
	public GameException() { }
	public GameException( string message ) : base( message ) { }
	public GameException( string message, Exception inner ) : base( message, inner ) { }
#if !SILVERLIGHT
	protected GameException(
		System.Runtime.Serialization.SerializationInfo info,
		System.Runtime.Serialization.StreamingContext context )
		: base( info, context ) { }
#endif
}

Xaml related classes

Silverlight – BooleanToVisibilityConverter doesn’t exist

OK, not hard to replicate (or borrow from many places on the Internet)
Someone’s probably building a nice platform levelling library with all these little missing things in. Both Silverlight and WPF are moving so fast that it’ll be a moving target. I already knew Commands were missing in Silverlight but available in the Prism framework and I’m sure there are other efforts

But how things stay the same

Xaml and the basic controls are all the same so I didn’t have to change my UI beyond copying the Xaml from my WPF form into my Silverlight page.

While there is no binary compatibility Silverlight 4 comes very close in many areas to source-code compatibility. The namespaces needed even match so you can just link the same .NET/WPF Model and even ViewModel .cs files into the Silverlight project.

In this case that was all I had to do beyond the two issues with my Exception subclass and copy in a BooleanToVisibilityConverter. All the rest of my code and xaml just worked.

So less than an hour I converted the project from WPF to Silverlight, with 90% of the code and xaml unchanged. Even for this simple game that was pretty good.

And here is the result. Enjoy!

No Comments

Leave a Reply

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