Welcome to WindowsClient.net | Sign in | Join

Rob Relyea - XAMLified

WPF, Silverlight and XAML

XAML: Using Generic Types in XAML 2009

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…

Published Monday, June 01, 2009 3:54 PM by Rob_Relyea
Filed under: ,

Comments

# XAML: Using Generic Types in XAML 2009 - Rob Relyea@ Tuesday, June 02, 2009 6:53 AM

Thank you for submitting this cool story - Trackback from DotNetShoutout

# Dew Drop - June 2, 2009 | Alvin Ashcraft's Morning Dew@ Tuesday, June 02, 2009 8:28 AM

Pingback from  Dew Drop - June 2, 2009 | Alvin Ashcraft's Morning Dew

# Daily Tech Links - June 3, 2009@ Wednesday, June 03, 2009 2:23 AM

Daily Tech Links - June 3, 2009 Web Development Web Browsers Need a Social Layer XAML: Using Generic

# Bea Stollnitz &raquo; Data virtualization@ Sunday, July 26, 2009 3:38 PM

Pingback from  Bea Stollnitz &raquo; Data virtualization

# re: XAML: Using Generic Types in XAML 2009@ Tuesday, August 11, 2009 8:31 PM

Awesome.  I need ObservableCollection and Dictionary now, but at least it's a feature that's coming.

by Carl Scarlett

# re: XAML: Using Generic Types in XAML 2009@ Monday, September 28, 2009 8:13 PM

Very nice! Any thoughts on how this trickles down to Silverlight?

# re: XAML: Using Generic Types in XAML 2009@ Tuesday, September 29, 2009 9:28 AM

Bryant-

Silverlight will likely implement x:TypeArguments when a markup scenario needs a generic type in it. If you have scenarios that badly need that, you should communicate those scenarios to us.

Thanks, Rob

# re: XAML: Using Generic Types in XAML 2009@ Monday, December 21, 2009 3:38 PM

Continuing to look at uses of generics in xaml, and found a slight regression in what x:Type accepts. In short, declarations like "{x:Type List`1}" no longer return the open generic type (List<> in this case), they just result in an error. You can see the full error message in the "Aside" at the bottom of this question: stackoverflow.com/.../styling-generic-wpf-controls (and if you have any feedback on the question itself, that would be appreciated as well :).

# re: XAML: Using Generic Types in XAML 2009@ Wednesday, January 13, 2010 6:28 PM

@Emperor XLII-

Thanks for finding this. We've filed a bug and are analyzing the right approach for .NET 4 on this.

-Rob

Leave a Comment

(required) 
(required) 
(optional)
(required) 
Page view counter