With the release of Silverlight 2 and now Silverlight 3, the gap between Silverlight and Windows Presentation Foundation (WPF) has shrunk by a large margin. That gap does still exist though and there are many scenarios where a light browser-based interface, a heavy feature-rich desktop interface, or both is warranted. It's that last scenario, where both a Silverlight and WPF version of an application will be developed, that I will be covering in this tip.
Linked files in Visual Studio
Sharing physical files by linking is a great feature of Visual Studio and without it we wouldn't get very far with sharing UI or code. This feature will come in handy whether you are developing an application, targeting Silverlight and WPF, or just want to organize some files shared among various projects. In practice you add an existing item to a project as a link much the same way you add a copy of an existing item. The "Add as Link" option is available in the "Add" dropdown of the "Add Existing Item" modal dialog.
Sharing UI: User controls
When I think about developing application targeting two platforms I often jump straight to sharing code and forget about the user interface, but a sharable UI is an important part of the Silverlight/WPF story. In the past it has been pretty straight forward to share design artifacts such as images and videos. Now with XAML you can take a big step forward sharing part or the entire actual application user interface. Most likely you won't be able to and won't want to share the entire user interface between Silverlight and WPF; this is why user controls are such a fit for the scenario.
Sharing code: Preprocessor directives, partial classes, and extension methods
I guarantee that your application will have some functionality that will be implemented differently in Silverlight versus WPF. An example would be the "Open File Dialog" where the Silverlight implementation is just different enough to force you to implement it for both platforms. These cases are certainly not showstoppers as there are several techniques you can use to cordon off these implementations from shared code.
Using the techniques described in this tip you will be able to easily write applications targeting both the Silverlight and WPF platforms sharing much of your UI and application code. Taking the concept of UI and code reuse further there are several more techniques, extension methods for example, and coding practices that will allow you to squeeze out even more shared UI and code between the Silverlight and WPF platforms.
Steven Porter is a Senior Consultant and Project Manager for Wintellect, a consulting/debugging and training firm founded by industry experts Jeff Prosise, Jeffrey Richter, and John Robbins. He specializes in technologies such as ASP.NET, ASP.NET Ajax, Silverlight, and WPF. Steve is a Microsoft MVP in Client Application Development and a frequent speaker at events such as user groups and code camps on topics ranging from Silverlight and WPF to Windows Communications Foundation.