XAML 2006 Background
XAML design in .NET 3 was mostly driven by WPF scenarios. In that timeframe, WPF only had one significant generic type that required its use in XAML: PageFunction<T>. As such, we supported x:TypeArguments on the root element of compiled XAML files. We knew there were 2 further steps for Generic support to do, but skipped them for v3:
- Support instantiation of a generic type in any XAML load. (coming in XAML 2009)
- Support providing x:TypeParameters to compiled XAML. (not coming in XAML 2009)
XAML 2009 Support for Generic Types
Since .NET 4 is seeing XAML usage broaden into other areas with System.Xaml.dll and XAML 2009 language features, it became more important for XAML to support Generic types (a CLR 2 feature!).
This example uses WPF generic types (just because I’m most adept with those tags):
<Window>
<Window.DataContext>
<ObservableCollection x:TypeArguments="my:Person">
<my:Person FirstName="Tom" LastName="Holiday" />
<my:Person FirstName="Joan" LastName="Holiday" />
</ObservableCollection>
</Window.DataContext>
…
</Window>
This could be accomplished in C# by:
Window win = new Window {
DataContext = new ObservableCollection<Person> {
new Person { FirstName="Tom", LastName="Holiday" },
new Person { FirstName="Joan", LastName="Holiday" }
}
};
(thanks to Dave's comment who gave me more concise C# syntax and Simon's comment who removed an extra "new").
Multiple Type Arguments
In order to support multiple type arguments, use a comma to delimit:
<Dictionary x:TypeArguments="x:String, x:Double">
…
</Dictionary>
Nesting of Type Arguments
If you wanted to have a Dictionary of Strings map to lists of Doubles, you would use parenthesis (since <> are kindof taken in XML):
<Dictionary x:TypeArguments="x:String, sys:List(x:Double)">
…
</Dictionary>
Node Stream Representation
For those of you looking at the nodes that XamlXmlReader (or other XamlReaders) produce, x:TypeArguments appears to disappear from the stream of nodes. This is by design…a XamlReader would expose the ObservableCollection element above as:
- xamlReader.NodeType: StartObject
- xamlReader.Type: ObservableCollection(my:Person)
In the PDC2008 XAML talk, we go over the Node Stream…but I haven’t blogged about it yet…
Remember
Most XAML 2009 Features won’t work in compiled XAML scenarios in .NET 4, but will work with uncompiled XAML. See details here…”Yes, XAML2009 isn’t everywhere yet…”