Seven New Features in Silverlight 5 (Plus a 3D Bonus)
The next version of Silverlight should be available by the time you read this article. The Silverlight team has followed a fast pace during the last few years, producing four versions of their framework in only thirty months. Each new version of Silverlight has been full of surprises and useful features. Version 5 is no different. Let’s look as some of the best features available in SL 5. XAML Debugging Gather a crowd of developers together and they’ll soon be grumbling about their daily code battles. Markup languages like HTML and XML often become a target for developer angst during these gripe-fests due to their lack of debugging support. Silverlight suffers from this problem and it’s particularly aggravating when using data binding in your XAML. I’ve seen the angry glint in my teammates’ eyes when they’re wrestling with a nasty data-binding problem. My favorite new feature in Silverlight 5 has to be the XAML debugging supported added to Visual Studio. Finally, it’s possible to place breakpoints in my XAML, press F5 to step through the binding markup and see binding details in the Visual Studio Locals window. To demonstrate this satisfying new feature, I’ll bind a ListBox and TextBlock to a simple data source and then add a breakpoint. First, let’s look at the data source classes. public class Food { public string FoodName { get; set; } public Int32 Calories { get; set; } public Int32 StockCount { get; set; }} public class Foods { private List<Food> foods; // ... constructor creates the list of Food public List<Food>FoodByCategory( string category){ var q = from f in foods where String.Equals(f.Category, category, StringComparison.OrdinalIgnoreCase) select f; return q.ToList();}
Next, I’ll create the UI and bind the controls. Note the typo on the TextBlock binding. <ListBox Height='163' ItemsSource='{Binding}' DisplayMemberPath='FoodName' Grid.Row='1' />
<TextBlock Grid.Row='2' Text='{Binding FodName}' />
Finally, I’ll set the data context in the code behind. var foods = new Foods(); this.DataContext= foods.FoodListByCategory("Dairy");
Because of the typo in the data-binding XAML, the bound data will not show up at runtime. This is the perfect scenario for XAML debugging. Setting a Breakpoint in XAML Setting a breakpoint in XAML is identical to the way you set one in the Visual Studio code editor. First, move the selection point to the line of XAML that you want to debug. Then either press F9 or click in the code selection margin on the left side of the code window. Note you cannot add a breakpoint to a line of XAML unless it has a Binding Markup Extension within that line of text. Figure 1 shows two breakpoints set in the XAML editor.  Figure 1: Two breakpoints in the XAML editor window.With the breakpoints in place, all you need to do is run a debug session for the application. After the debugger is attached, you will see the joyful sight shown in Figure 2.  Figure 2: Waiting at breakpoint and examining the Locals window.Once you are at the breakpoint, you can view the Locals and other debugging windows in Visual Studio. You can also step through the code files while doing XAML debugging. You can customize the breakpoints as Microsoft ported all the normal breakpoint options to the XAML version. Just right-click the red breakpoint circle and set breakpoint conditions, filters and hit counts as shown in Figure 3.  Figure 3: Breakpoint options. | 
By: Walt Ritscher
Walt Ritscher has trained thousands of corporate developers during the last twelve years. An active speaker, his teaching schedule has taken him throughout the world providing developer training at corporations, universities, and software conferences. He has collaborated on several books and videos published for the developer market including early adopter .Net courses at Microsoft Press. Walt is currently consulting and teaching .NET and WPF classes for Wintellect. Walt's industry expertise has placed him on various national technology advisory boards. He is also deeply involved in the local developer community-founding the .NET Developers Association in Redmond, WA. Walt has accumulated plenty of experience as a developer and is currently a Microsoft MVP and a member of the Silverlight Insiders. As a Web programmer he has worked numerous projects including: EPA sites and the Microsoft Community Starter Kit.
waltonline@scandiasoft.com |