Migrate/convert WPF to Silverlight

The theory states that Silverlight is a subset of WPF, so it is likely to think that it is an easy task to convert a WPF Application to Silverlight.

I’m in the process of doing this task – migrating from WPF to Silverlight – and it is not an easy one. Actually, I would recommend that the two technologies, WPF and Silverlight, are treated as completely distinct platforms. That is, you should never expect to be able to switch freely between them.

September 2nd 2010 update: The more I work with these two technologies, the more it becomes clear to me that this is a crucial point! WPF and SL are very much completely different technologies and requires different developer mindsets. The cool thing is that it is easy as a developer to move between the two platforms, but it is however not easy to move an application between the two platforms. If you are an architect and think it would be cool to have a WPF client because “then you can always switch to Silverlight later”, then you are missing the point.

So far in the progress I have noted the differences below. I thought that they might be relevant to others. As my work progresses, I’m sure I’ll locate more differences, and at that point I’ll update this post.

ToolBar control
The toolbar functionalities are completely missing. However, Telerik has some (not free) stuff, and a guy at codeplex has developed a prototype of a Ribbon. However, don’t expect anything out of the box.

ComponentResourceKey object in System.Windows is missing
If you don’t know what this is, you’re not going to miss it. However, I created a global resource framework based on this class, and I had to rewrite the whole stuff when moving to Silverlight.

Statusbar uses host (IE)
If you need a status bar, you can either write one yourself, or you can use the statusbar in IE. Unless, you want Silverlight to run out of browser (I think). Nothing out of the box here neither.

No xps-document viewer
This was actually a quite critical functionality in my app. There are some (not-free) controls out-there, but when you read about them you don’t get the ‘stable code impression’ that you would like from your 3rd party vendors. Actually, I originally decided on XPS because my guess was that Microsoft in general would do a lot to support this format. But when I migrated to silverlight I actually switched my XPS document format out with PDF format to be rendered in a browser control as a workaround.

Assemblies cannot be re-used directly
This is a very important point. A WPF app and a Silverlight app cannot share the same dll! This means that if you have a WPF class library, you should expect to copy-paste the code to a new Silverlight library and rebuild it. Visual Studio does however come with a possibility of creating a Silverlight library and link to the .cs-files in the WPF library – i.e. you will have two libraries, but only one set of .cs-files, since one of the libraries is linking to the other.

However, after playing around with this, I would recommend in most cases to eat the redundancy and copy-paste the code :-/ But this also points to a crucial point – do not treat the two technologies as if Silverlight is merely a subset of WPF. I really do not consider it to be so. Way to many design decisions, both in your UI and in your code, will be different and platform dependent.

No sharing between client and server
In some situations you would might like to share libraries between the client and the server. Do not expect this to be possible since SL cannot read WPF class libraries.

WebServices in assemblies cannot be linked.
As I wrote a couple of paragraphs ago, it is possible to create .cs file links. However, you cannot link a webservice in one class library to another. Expect that you will have to generate all your service references all over.

System.Configuration is not supported
Nope, it’s true – it’s not included. I really don’t understand why. Jonas Follesøe has a blog post where he suggests an approach for writing your own.

IExtensibleDataObject is not supported.
This interface is used by Visual Studio when generating code for proxy clients in WPF. It’s not included in Silverlight. If you have made a partial class and implemented this interface, for example with the purpose of extending your proxy classes, you will have to refactor this.

XmlDictionaryReaderQuotas can only be set to max
And thus it doesn’t make sense to set this propert in Silverlight. As far as I can see, it has a different signature also, but I haven’t investigated it more in depth – I only noticed that my configuration factory stopped working ;-D

No wsHttpBinding
wsHttpBinding is not supported in SL. In Silverlight 4 netTcp is supported, however. What makes the missing support for wsHttpBinding a bit awkward, is that this is the default binding in the WCF world, but if you use Silverlight you could consider basicHttpBinding or netTcp. This also means that classes like ReliableSessionBindingElement are not supported.

That was it for now:-)

UPDATE September 2nd 2010:

Silverlight can handle only PNG and JPG images. This has some implications: For instance multi page TIFF files is not possible to handle in Silverlight. Also animated GIF’s are not possible. Also, you cannot use the “frame”/animation feature of PNG files in Silverlight. Only static images. I had to display multipage TIFF-documents for preview, and I made a service on the server which grapped the document from the database and at runtime created a PNG array which was returned to the client.

No mouse-wheel
Mouse wheel is not supported in silverlight out of the box at least. I recommend this approach instead.

No standard way to get user name
Now – this is not a joke – many clever people suggests implementing a webservice on a server, disabling anonymous authentication, forcing the browser to authenticate the user, decoding the user from the asp.net httpcontext on the server and returning the user name to the Silverlight client. Sigh! Such workarounds did not appear because people like to make it hard to obtain the username, but because it is hard to obtain the username from within Silverlight. Another approach is to grap the username from the windowsidentity within the asp.net hosting page. Setting it in a hidden html field, and retrieving the value from that hidden field from within the Silverlight client. Isn’t the reason that we have a framework to avoid silly hacks like this? I’m aware of all of the good reasons why this is hard – including cross browser compatibility and cross os compatibility, but come on guys – it’s just a freakin’ username, I want 🙂

UPDATE May 2011

BooleanToVisibilityConverter missing in Silverlight. Doh! I had a minor change request that I wanted to throw in with a minimal effort. Luckily I found out that .NET 4.0 provides a BooleanToVisibilityConverter – well, great – this is the stuff that I expect from a good framework – it seems really quite stupid to write stuff like this yourself since everyone will need it sooner or later. I upgraded my Silverlight App to .NET 4.0 (making sure with our IT Operations guys that they support .NET 4.0 on our client pc’s – they didn’t the last time I deployed), and then … sigh! This feature is not supported in Silverlight, only in WPF. But why???? Because of the well-known security risk associated with hiding and displaying controls (being ironic)? Anyway, it ain’t there, write your own 🙂


5 responses to “Migrate/convert WPF to Silverlight

  1. Thanks for posting this! It’s exactly what I was looking for. I would have expected that migration would be a piece of cake, for the reasons you mentioned. I’ll now definitely think twice before starting and will probably just keep it in WPF – as that works oke.

  2. Thank you for this article. I am working on a similar one and found this post very useful. Great job.

  3. Great stuff! A lot of people think it would be easy. This is a good checklist to go through.

  4. No way Jack. It took me less than 2 days to convert a substantial SL LOB app to WPF. We didn’t use any XAML though – all done in code. Very easy that way.

  5. @Bru/Jack(?), thanks for you comment. Please, this is not a binary yes/no question, that I am addressing, and the intention of this blog post was never to be provocative. I’m glad that it took just 2 days for your conversion – you are probably a clever guy. I’ve converted a WPF-2-SL app in also just a couple of days in one case as well – of cause this can be done, but frankly I think that you are totally missing my point, beingg: These are different platforms! SL is a web platform (or smart phone platform), and WPF is a Windows client platform. Never expect to just be able to convert freely between them – you might be able to, but do not base your platform decision on a “SL is a subset of WPF, so we’ll always be able to switch”-mindset. Because, this is not reality. Also, this is not the real strength of SL being a subset of WPF. Architects who think so are mistaken big-time and are missing the beauty of this construction. The real strength is that developers can quite easily switch between the technologies as they will only have to learn XAML once, they will only have to learn the event model once, they’ll only have to learn the framework once and so on.

    Finally, if you have an app containing just a few thousand lines of code, yes, probably this will never be a major issue – at least not one that cannot be solved in a couple of days, but the more you are thinking Enterprise App, the larger problems you will run into, the more you are integrating with browser or OS, the more problems you’ll run into, the more services you are integrating to, the more problems you’ll run into, the more different ressources you are using, the more problems you’ll run into, the more internationalization and configuration you need, the more problems you’ll run into and so on.

    Please use my blog post as (1) documentation for the problems that I already have encountered, and (2) as my opnion on what SL and WPF is and how you should be thinking about them.

    Please do not use my blog post as a one-dimmensonal answer to whether or not it is possible or expensive to convert between SL and WPF.

    Br. Morten

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s