MSDN Magazine - October 2008 - (Page 23) BEth Massi Basic instincts Dynamic Data Entry With XML Literals Wouldn’t it be nice to generate all your maintenance screens in your data-driven applications automatically? When I say “maintenance” I mean all those simple lookup data tables or contact tables. Instead of handing those screens to the junior developer on the team, why not just generate them all at run time based on your object model or database schema? This is a relatively easy task in Windows Presentation Foundation (WPF) using XAML, and with the deep XML support in Visual Basic it becomes a snap to generate user interfaces dynamically. to use it. This allows you to reserve the default namespace for other XML you may be manipulating in your code: Imports Imports Imports System.Windows.Markup Class Window1 Private Sub Window1_Loaded() Handles MyBase.Loaded Dim UI = This is COOL! Me.Content = XamlReader.Load(UI.CreateReader()) End Sub End Class Dynamically Loading XAML Dynamically creating WPF user interfaces at run time is easy using Visual Basic XML Literals and XML namespace imports. This is because WPF expresses its user interface elements and layout in XAML and Visual Basic does all the heavy lifting, resolving the XML namespaces in order to create the correct XAML. Let me show you what I mean. You can load and save XAML at run time using the XamlReader and XamlWriter classes in the System.Windows.Markup namespace. Create a new WPF project and, in the Window1 codebehind, you can write this: Imports Imports Imports System.Windows.Markup Class Window1 Private Sub Window1_Loaded() Handles MyBase.Loaded Dim UI = This is COOL! Me.Content = XamlReader.Load(UI.CreateReader()) End Sub End Class One caveat here is that the XAML you load at run time cannot require compilation, so you cannot specify event handlers on dynamically loaded controls in the XAML directly. You can still add them at run time in the codebehind. For example, you can’t do this dynamically, as it will cause a XamlParseException “Must compile XAML file that specifies events:” Dim UI = This is COOL! Me.Content = XamlReader.Load(UI.CreateReader()) Instead, you can get the reference to the label at run time and explicitly add the event handler in code: Private Sub Window1_Loaded() Handles Window1.Loaded Dim UI = This is COOL! Dim myLabel As Label = XamlReader.Load(UI.CreateReader()) Me.Content = myLabel AddHandler myLabel.MouseDown, AddressOf Label1_MouseDown End Sub Private Sub Label1_MouseDown() MsgBox("Mouse Down!") End Sub You’ll see the label populate the contents of the window, as shown in Figure 1. When you include the WPF namespace imports at the top of the code file, Visual Basic automatically creates the proper XAML you need for it to load at run time. In this example I’m using the default namespace for the controls; however, you can also provide a namespace Figure 1 XamlReader Loads prefix and change the XAML XAML at Run Time As you can see, there are many possibilities. Of course, dynamically generating a UI isn’t unique to WPF. You can do this in Windows Forms as well, but it is an exercise in coding the layout by hand. WPF and Visual Basic make this a breeze because you can define one piece of XAML and construct it from a single LINQ query. Obtaining the Metadata Before you can construct the XAML for your UI you’re going to need some metadata. You need an easy way to describe how your Send your questions and comments to instinct@microsoft.com. October 2008 23
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.