MSDN Magazine - October 2008 - (Page 34) es affect mostly the compatibility at the XAML level. For complete compatibility, you can use the common subset, which, thankfully, is large enough to still let you do virtually anything. Sharing Code When VSM becomes available for the desktop version of WPF, you’ll be able to use the same approach for both Silverlight and Windows desktop applications and share control templates between WPF and Silverlight projects. Until then, let’s see how you can reuse code between WPF desktop and Web projects today. A WPF app is made of XAML and managed code. The managed code targets the classes of the supported version of .NET. It is your responsibility to use a common subset of XAML that both desktop WPF and Silverlight 2 can understand. Likewise, it is your responsibility to organize your codebehind classes such that differences in the back-end framework are handled properly. There are basically two scenarios in which you’ll want to reuse WPF code. One is where you have an existing WPF desktop application and want to offer it over the Web to simplify maintenance and deployment. The other is when you want to develop a front end for an existing system and offer it for both Windows and Web clients. I’ll approach the question of reusing code from the WPF-toSilverlight perspective. In terms of patterns to refactor code and markup, there’s very little difference. Reasoning about WPF Applications A typical WPF application is built from a tree of objects where Window is the root of the tree. In turn, the Window element contains a number of child elements laid out or stacked in a variety of ways. Elements refer to basic shapes, layout managers, storyboards, and controls (including custom third-party and user controls). Here’s a basic example: This code cannot be incorporated into a Silverlight 2 application as is. First of all, the Window element is not supported in Silverlight. In the Silverlight assemblies, there is simply no such class in the System.Windows namespace. The root tag of any Silverlight application is UserControl. The element is mapped to the UserControl class defined in the System.Windows.Controls namespace. Here’s the modified heading of a Silverlight application: All the markup code you have within the boundaries of the and elements should continue to match. It is up to you to modify the original desktop XAML to make it match 34 msdn magazine Cutting Edge http://www.scaleoutsoftware.com
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.