Home > Microsoft .Net Development Tips > > How to develop applications targeting both Silverlight and WPF
Win Development Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 


How to develop applications targeting both Silverlight and WPF


Steve Porter, Consultant
10.07.2009
Rating: --- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


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.

Now instead of your project containing a physical copy of the existing item, it contains just a logical link to the physical location of the existing file. From this point forward, changes made to the file via either the primary or secondary project will be reflected in the other project. It's also worthwhile to note that there is nothing preventing a file from being shared with more than one project.

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 ta...


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
Silverlight and Expression application development
Microsoft lightens up to Eclipse for PHP, Azure related application building
How to encode video markers for consumption in Silverlight and WPF
How to access cross domain Web services from Silverlight without a policy file
Microsoft takes on Adobe with Silverlight 3
How to write an out-of-browser Silverlight 3 application in 3 steps
Silverlight 3 beta SDK download lets developers try new RIA features
Silverlight plug-in for Eclipse highlights Microsoft's work on interop
Microsoft announces Silverlight 3, Expression Blend 3 at MIX09
Moonlight 1.0 release brings Silverlight to Linux
Silverlight, Ajax components require different approach to UI

Windows Presentation Foundation
How to speed up Visual Studio 2008's slow WPF designer
How to list fonts in WPF using markup extensions and data templates
Fast guide: Silverlight blogs
Modeling tool provides 3D components for WPF apps
VantagePoint WPF Controls delivers rich suite of UI components
Mobiform releases WPF component set
ComponentOne adds rich-text editor to Studio Enterprise
Windows Developments: Product news, December 2007
Book Excerpt: What's new in Windows Presentation Foundation
What Visual Studio 2008 brings to client application development
Windows Presentation Foundation Research

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
Expression  (SearchWinDevelopment.com)
Expression Graphic Designer  (SearchWinDevelopment.com)
Expression Interactive Designer  (SearchWinDevelopment.com)
Expression Web Designer  (SearchWinDevelopment.com)
Moonlight  (SearchWinDevelopment.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary


ke 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.

User controls already provide a level of modularity for your UI. If you keep that modularity in mind when designing your application you will be able to get a large amount of reuse from user controls. To achieve this there are two main themes to keep in mind. One is to stick with the subset of XAML that Silverlight supports. You will find this to be very doable with the XAML support in the latest versions of Silverlight and when there is an exception there will just be a piece of your UI that remains separate for the two platforms. The second theme is to place little or no code in the code-behind file associated with your user control. This is a best practice in general and part of the MVVM design pattern, so it will make your life much easier in the long run.

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.

Probably the most used technique, since it has been around longest, is preprocessor directives. Preprocessor directives are embedded commands allowing you to instruct the compiler to conditionally compile certain sections of code. Silverlight already has defined a Boolean preprocessor directive symbol for allowing you to know when an application is being compiled for Silverlight. Using this technique, all Silverlight code would be placed in an If statement checking the SILVERLIGHT symbol and any WPF code would be placed in the Else branch of the If statement.

Using preprocessor directives is an easy solution but is not my favorite because you end up with both Silverlight and WPF code sections intermingled within the same files and classes. A much more elegant and better organized technique is to use partial classes to separate shared code from platform specific code. Starting with version 2.0, the .Net framework implemented a feature called partial classes. Prior to compile time, partial class functionality allows the implementation for a since class to be separated into several "partial" classes split over several files. In a Silverlight/WPF scenario, this allows you to have functionality specific to each platform implemented via partial classes in separate files. All shared code would remain in one physical file linked between the projects and at compile time the platform specific implementation would be injected into single classes.

Conclusion
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.

About the author
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.

Rate this Tip
To rate tips, you must be a member of SearchWinDevelopment.com.
Register now to start rating these tips. Log in if you are already a member.




DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



Database Programming Solutions - .NET XML, Visual Studio LINQ, ORM .NET
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2000 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts