Bookmark and Share Share...    Subscribe to this feed Feed   About Christian Moser  


What's new in XAML of .NET 4.0

Easy Object References with {x:Reference}

Built-in Types

Generics in XAML with x:TypeArguments

Support for Arbitrary Dictionary Keys

Use of Non-Default Constructors with x:Arguments

Use of Static Factory Methods with x:FactoryMethod



With .NET 4.0 Microsoft will bring up a improved version of XAML. This arcitle shows you the language enhancements they made.

Easy Object References with {x:Reference}

If you want to create an object reference today you need to do a databinding and declare the source with an ElementName. In XAML 2009 you can use the new {x:Reference} markup extension

 
<!-- XAML 2006 -->
<Label Target="{Binding ElementName=firstName}">FirstName</Label>
<TextBox x:Name="firstName" />
 
<!-- XAML 2009 -->
<Label Target="{x:Reference firstName}">FirstName</Label>
<TextBox x:Name="firstName" />
 
 

Built-in Types

If you want to add objects of simple types like string or double to a resource dictionary today you need to map the needed clr-namespaces to an XML namespaces. In XAML 2009 we a lot of simple types that are included in the XAML language.

 
<!-- XAML 2006 -->
<sys:String xmlns:sys="clr-namespace:System;assembly=mscorlib >Test</sys:String>
 
<!-- XAML 2009 -->
<x:String>Test</x:String>
 
 

The following types are included into the XAML language:

  • <x:Object/>
  • <x:Boolean/>
  • <x:Char/>
  • <x:String/>
  • <x:Decimal/>
  • <x:Single/>
  • <x:Double/>
  • <x:Int16/>
  • <x:Int32/>
  • <x:Int64/>
  • <x:TimeSpan/>
  • <x:Uri/>
  • <x:Byte/>
  • <x:Array/>
  • <x:List/>
  • <x:Dictionary/>

Generics in XAML with x:TypeArguments

If you want to use an ObservableCollection<Employee> in XAML you need to create a type that derives from ObservableCollection because you cannot declare it in XAML. With XAML 2009 you can use the x:TypeArguments attribute to define the type of a generic type.

 
<!-- XAML 2006 -->
class EmployeeCollection : ObservableCollection<Employee>
{
}
 
<l:EmployeeCollection>
    <l:Employee FirstName="John" Name="Doe" />
    <l:Employee FirstName="Tim" Name="Smith" />
</lEmployeeCollection>
 
<!-- XAML 2009 -->
<ObservableCollection x:TypeArguments="Employee">
    <l:Employee FirstName="John" Name="Doe" />
    <l:Employee FirstName="Tim" Name="Smith" />
</ObservableCollection />
 
 

Support for Arbitrary Dictionary Keys

In XAML 2006 all explicit x:Key value were threated as strings. In XAML 2009 you can define any type of key you like by writing the key in ElementSyntax.

 
<!-- XAML 2006 -->
<StreamGeometry x:Key="CheckGeometry">M 0 0 L 12 8 l 9 12 z</StreamGeometry>
 
<!-- XAML 2009 -->
<StreamGeometry>M 0 0 L 12 8 l 9 12 z
    <x:Key><x:Double>10.0</x:Double></x:Key>
</StreamGeometry>
 
 

Use of Non-Default Constructors with x:Arguments

In XAML 2006 objects must have a public default constructor to use them. In XAML 2009 you can pass constructor arguments by using the x:Arguments syntax.

 
<!-- XAML 2006 -->
<DateTime>00:00:00.0000100</DateTime>
 
<!-- XAML 2009 -->
<DateTime>
    <x:Arguments>
        <x:Int64>100</x:Int64>
    </x:Arguments>
</DateTime>
 
 

Use of Static Factory Methods with x:FactoryMethod

When you have a type that has no public constructor but a static factory method you had to create that type in code in XAML 2006. With XAML 2009 you can use the x:FactoryMethodx:Arguments attribute to pass the argument values.

 
<!-- XAML 2006 -->
Guid id = Guid.NewGuid();
 
<!-- XAML 2009 -->
<Guid x:FactoryMethod="Guid.NewGuid" />
 
 




Last modified: 2009-11-01 10:52:07
Copyright (c) by Christian Moser, 2011.

 Comments on this article

Show all comments
Commented on 22.September 2010
Asif
Commented on 5.October 2010
I also Didn't got the topic !!!! for what it is
pallavi
Commented on 20.October 2010
Nice Article
lll
Commented on 20.October 2010
se
rahul
Commented on 20.October 2010
yak................dirty
sp
Commented on 9.November 2010
uper kiye gye sare comment bakwaas hain
Kiran
Commented on 10.November 2010
Hi Very nice article, i am using VS2010 C# expression Edition
When i am using this markup
<Label Target="{x:Reference firstName}">FirstName</Label>
<TextBox x:Name="firstName" />
i got warning "Service Provider is Missing the INameResolver Service"
Any idea how to resolve this???
John
Commented on 18.November 2010
Here is the explanation from Adam Nathan's WPF 4 unleashed book:
"The x:Reference markup extension is often mistakenly associated with the XAML2009
features that can only be used from loose XAML at the time of this writing. Although
x:Reference is a new feature in WPF 4, it can be used from XAML2006 just fine as long as your project is targeting version 4 or later of the .NET Framework.
One glitch is that the XAML designer in Visual Studio 2010 doesnât properly handle x:Reference, so it gives the following design-time error that you can safely ignore:
Service provider is missing the INameResolver service"
Harish
Commented on 15.December 2010
nice features...nice explanation
vishal
Commented on 22.December 2010
Above Example and Definition is very good & sort and sweet.
basavaprasad
Commented on 14.January 2011
Really nice Explanations.....
sohan
Commented on 1.February 2011
didnt understand any thing.please explain in detail with examples..
orhor
Commented on 25.February 2011
thank you for mentioning, but VS2010 doesnt offer a namespace url like "http://schemas.microsoft.com/winfx/2009/xaml"
or where should I set the version?
Commented on 2.March 2011
DNM
Commented on 14.March 2011
This still does not work in WPF and VS 2010 !
DNM
Commented on 14.March 2011
This still does not work with WPF and VS 2010 !!!
kkk
Commented on 14.March 2011
mmmm
Anup
Commented on 1.April 2011
Good for beginer...
kamlendra
Commented on 11.April 2011
please provide the examples along with the syntax.you can provide the link to the example.
Mohit Jethva
Commented on 20.June 2011
It's very nice feature.. Keep it up...It's very useful for beginer.
Deepthi
Commented on 8.July 2011
Good one
Tim Valentine
Commented on 10.July 2011
http://msdn.microsoft.com/en-us/library/ee792002.aspx
Raj
Commented on 4.August 2011
Hi I think this site is useful, but it will be more useful if there are more examples to explain the concepts,as we are beginners. Thanks in advance.
Rajeev Kumar
Commented on 19.September 2011
I did not get the topic.Can you please explain it little bit more.
anand
Commented on 21.September 2011
hai... i need eg.,

Name
E-Mail (optional)
Comment