MSDN Magazine - October 2008 - (Page 33) DINO ESPOSITO Cutting EdgE Code Reuse In WPF And Silverlight 2 In Silverlight 2, you use Extensible Application Markup Language (XAML) to design and render the user interface. At the same time, you leverage the built-in core CLR to process managed code within the browser. As a result, there’s a strong similarity between Web-based Silverlight 2 applications and desktop Windows Presentation Foundation (WPF) applications. One goal of having a similar programming model is that it enables easy code reuse between the two. In this column, I’ll discuss a few patterns to make sharing code and XAML markup between Silverlight 2 and WPF as easy as possible. and behavior of WPF controls through templates and styles. For instance, you could not only change the shape and appearance of a control but also define a new action or animation for when the control is clicked, gets or loses focus, and so on. With VSM, visual states and state transitions do some of that work for you. Typical VSM visual states are normal, mouse-over, disabled, and focused. For each of these states you can define a style WPF and Silverlight 2 Compatibility With the introduction of Silverlight 2, XAML is poised to become the API for a new generation of UI modules. Silverlight 2 supports a subset of the full WPF framework including features for rich layout management, data binding, styles, media, animation, graphics, and templates. However, full support for XAML in Silverlight 2 is constrained by the size of the downloadable plug-in. In just a few megabytes (less than 5MB in Beta 2), the Silverlight 2 plug-in must deliver the core CLR, a version of the Microsoft .NET Framework 3.5 that includes WPF and a subset of the Windows Communication Foundation (WCF) client platform, a XAML parser, and a number of Silverlight-specific controls. In Silverlight 2, you won’t find support for the WPF 3D graphics capabilities, and you may find that some attributes and elements have been dropped or trimmed down. At the end of the day, though, you have a compatible subset, so you can produce a Silverlight version of a moderately complex WPF user interface. Shortly, I’ll return to discuss critical areas of compatibility. Note, however, that Microsoft is also adding some new APIs in Silverlight that may not currently have a counterpart in the desktop version of WPF. The most relevant features include controls, such as the DataGrid, and classes, such as WebClient for simple GET-style network calls. These features will be added to WPF too. Silverlight 2 supports a subset of the full Windows Presentation Foundation. or a shape. State transitions define how the control moves from one state to another. You typically define transitions through animations. At run time, Silverlight will play the appropriate animation and apply the specified style to elegantly and smoothly move the control from state to state. To define a visual state, insert a fragment of markup in the control template, as shown here: Introducing Visual State Manager Each control defines one or more groups of states such as CommonStates and FocusStates. Each group, in turn, defines specific visual states such as MouseOver, Pressed, and Checked. For each visual state and transition between states, you can define a storyboard that Silverlight will automatically play when appropriate. In a nutshell, there are features in WPF that Silverlight 2 doesn’t support and features in Silverlight 2 that WPF lacks. These differencThis column is based on a prerelease version of Silverlight 2. All information herein is subject to change. Send your questions and comments for Dino to cutting@microsoft.com. Code download available at msdn.microsoft.com/magazine/cc135911. October 2008 33 Another feature that ships in Silverlight 2 and will be added to WPF is the extremely cool Visual State Manager (VSM) for controls. VSM simplifies the development of interactive control templates by introducing visual states and state transitions. Since the introduction of WPF, developers could customize the look, feel, http://msdn.microsoft.com/magazine/cc135911
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.