I was Never Much Good at Hello’s
Until recently I’ve been so awe-struck by the massive changes presented by .NET 3.x that I’ve avoided in-depth research into its bounty. Windows Presentation Foundation is a major paradigm shift that, frankly, intimidates me. But lately I’ve been digging in around the edges trying to understand what these changes could mean for the future of .NET solution designs.
I finally caved and got myself a copy of VS2008 Beta 2 and am really excited about it. Particularly, I’ve gotten to play with cool new language features in Visual Basic 9.0. Now, I’d move to VB9 for local type inference and object initializers alone but this weekend another feature piqued my interest: XML Literals.
In VB9 you can declare XML fragments and document inline in code like so:
Dim telephone = _
<phoneNumber>
<areaCode>800</areaCode>
<body>867-5309</body>
<extention/>
</phoneNumber>
You can access nodes in the fragment using a likewise intuitive syntax:
If telephone.<areaCode>.Value = “800” Then
... 'Something about toll-free calls.
End If
As anyone who’s ever worked with the XmlReader/XmlWriter can tell you this is a vast improvement.
So I got to thinking – VB9 can quickly declare XML literals… XAML (the markup language behind WPF and Workflow Foundation) is XML…
Putting 2 and 2 together I whipped up this little proof of concept application.
1. Open VS2008 and create a new WPF Application.
2. Change the default child node of the Window1 that’s generated from a Grid to a Canvas and set its name to “root” as below:
<Window x:Class="Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300">
<Canvas x:Name="root">
</Canvas>
</Window>
3. Now open your code-behind file and paste over it with the code below:
Imports <xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
Imports <xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
Imports System.Windows.Markup
Class Window1
Private Sub Window1_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
Dim data = From m In GetType(String).GetMethods() Where Not m.IsSpecialName Group By Name = m.Name Into Methods = Group, Count() Order By Name
Dim presentation = _
<ScrollViewer Height="300px">
<TreeView>
<TreeView.Items>
<%= From g In data Select _
<TreeViewItem Header=<%= g.Name & "() Method" & If(g.Count > 1, " - " & g.Count & " Overloads", String.Empty) %>>
<%= From m In g.Methods Select _
<TreeViewItem Header=<%= m.Name & "() : " & m.ReturnType.Name %> Background=<%= If(m.IsStatic, "Gray", "Black") %>>
<%= From p In m.GetParameters() Select _
<TreeViewItem Header=<%= p.Name & " : " & p.ParameterType.Name %>/> %>
</TreeViewItem> %>
</TreeViewItem> %>
</TreeView.Items>
</TreeView>
</ScrollViewer>
Me.root.Children.Add(DirectCast(XamlReader.Load(presentation.CreateReader()), UIElement))
End Sub
End Class
4. Hit F5 to run your project and you should see output like following:
In the end this really brings some of the expressiveness of ASP.NET to the WPF world. Looking at the declarative opportunities XAML opens up in WPF, WF, and even custom .NET objects – What are some cool things you can do with this? Please share your ideas.
-ADG
September 26, 2007
·
Anthony D. Green ·
No Comments
Tags: vb.net, wpf, xml · Posted in: Uncategorized

Leave a Reply