MSDN Magazine Launch Issue - February 15, 2008 - (Page 91) Silverlight Tips, Tricks, and Best Practices JEFF PROSISE U nless you’ve been stuck on a desert island for the past several months, you’ve undoubtedly heard about Silverlight™, the new cross-browser, cross-platform solution from Microsoft that helps you build rich Internet applications and rich, immersive media experiences in the browser. Version 1.0, which combines a XAML rendering engine with a JavaScript API, shipped in September 2007. Version 2.0, which will enter beta soon, will pair an enhanced XAML engine with a version of the CLR that runs in the browser, a Silverlight version of the Microsoft® .NET Framework Base Class Library (BCL), and a managed API. (That’s right: C# in the browser!) Silverlight 1.0 applications are proliferating on the Web, thanks in part to some of the high-profile companies that adopted it early and helped spread the love. The programming model is simple enough for an experienced developer to begin building Silverlight applications in a matter of hours. What’s missing for this still very young platform, however, is that wealth of knowledge and resources that accompanies more mature platforms. I’ve been developing Silverlight apps for more than a year now and have put together a list of best practices that I and other developers on my team use to build better applications. This column contains some of the most helpful ones. One best practice that I would add to that document is to avoid redundant calls to FindName. I often see event handlers structured this way: function onClick(sender, args) { var rect = sender.findName(‘Rect’); rect.fill = ‘red’; } The problem is that FindName has to walk the tree of XAML objects to find its target. If you’re going to be referencing the XAML object named “Rect” many times over the lifetime of the app, you should initialize it once—for instance, in an event handler for the root canvas’s Loaded event—and store the reference in a global variable. Then, instead of calling FindName every time onClick is called, you can do this: function onClick(sender, args) { _rect.fill = ‘red’; } Take Performance Tips to Heart Not long before Silverlight 1.0 shipped, Microsoft published a document titled “Performance Tips for Silverlight-Based Applications” (msdn2.microsoft.com/bb693295). This guide is a compilation of tips for optimizing performance by avoiding some common mistakes. The best practices you’ll find include: • Use the Visibility property rather than Opacity to hide objects. • Don’t use the Width and Height properties of MediaElement and Path objects. • Detach programmatically registered event handlers after use. • Use transparent control backgrounds sparingly. Most of these make sense when you read them. It’s not hard to imagine why it’s better to encode videos using the width and height at which you intend to display them (rather than encode them at one scale and display them at another). It’s because the Silverlight rendering engine can avoid resizing each frame on the fly. Still, it’s good to see them written down and to know that Silverlight doesn’t do something fancy to resize video without incurring a performance penalty. The resulting code will be faster than the original. But if a developer does this, he should be sure to release that reference when finished with the plugin. In normal pages this is never an issue, but in an AJAX app where whole pieces may be coming and going, its crucial to release those references to avoid a memory leak. For that reason, the Sys.UI.Silverlight.Control ajax type that is used when using the ASP.NET Silverlight Control (currently available in the Extensions 3.5 Preview Community Technology Preview) provides a pluginDispose method you can override to release references and event handlers. Figure 1 Default Silverlight Installation Experience launch2008 91 http://ASP.NET http://msdn2.microsoft.com/bb693295
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.