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
Tore Aurstad
Commented on 9.May 2009
Can I make use of these new features in Visual Studio 2008? When will the new .NET 4.0 features of WPF be available for developers outside Microsoft?
Christian Moser
Commented on 10.May 2009
They are all part of .NET 4.0. You need Visual Studio 2010 to use it. You can download a CTP version from here: http://www.microsoft.com/downloads/details.aspx?FamilyId=922B4655-93D0-4476-BDA4-94CF5F8D4814&displaylang=en. It's a virtual PC image. The beta version that will be released soon will be an installable version.
Jan Kučera
Commented on 16.May 2009
Do the non-default constructor arguments has to be in order of the consctructor signature? Or how do I specify which arguments are which? (And how does it work together with optional arguments?)
Theo Zographos
Commented on 19.May 2009
Nice changes, especially the added namespaces, but overall I wouldn't call them earth-shattering. Would be nicer to see simple arithmetic in XAML for example.
Christian Moser
Commented on 19.May 2009
Hi Theo,

you are right. I often missed arithmetic functions in XAML too. Maybe there is another clever way to do such things - for example with a MarkupExtension.
Prashant
Commented on 4.June 2009
Great article .. thanks for all that info
xeonix
Commented on 20.June 2009
cool, all XAML improvements in single article.
that's interesting, i'll try them...
San
Commented on 29.June 2009
Cool.. really informative..
Ajay
Commented on 27.August 2009
Brilliant!!
Muthu
Commented on 28.August 2009
If you add Previous and Next buttons in all the pages it will be more user fiendly
Christian Moser
Commented on 28.August 2009
Hi Muthu,
thanks for the excellent idea. I will do this as soon as I find time.
Grammar Nazi
Commented on 4.September 2009
Couple of wierd spelling mistakes. Nice and informative though!
nayak14
Commented on 31.October 2009
I have VS2008. I wanna use XAML 2009. how can install it?
nayak14
Commented on 31.October 2009
I have VS2008. I wanna use XAML 2009. how can install it?
Christian Moser
Commented on 1.November 2009
Hi Nayak14,

unfortunately it is not possible. The new XAML features are part of .NET 4.0 and this is only included in VS2010. You can download the free Beta 2 Bits and get startet.

http://www.microsoft.com/downloads/details.aspx?FamilyID=a80dfb5d-51c6-4778-8656-a9ff29d3a132&displaylang=en


Greetings
Christian
Nayak14
Commented on 2.November 2009
Thanks a Bunch!!!
Nayak14
Commented on 2.November 2009
Thanks a Bunch!!!
Don Wingate
Commented on 27.November 2009
What about defining a DataTemplate with a DataType that is a generic type? Is this still not possible in 4.0?
John D
Commented on 28.November 2009
How do you enable xaml 2009 in vs2010 beta 2? From the list of schemas I am only seeing http://schemas.microsoft.com/netfx/2009/xaml/presentation but no http://schemas.microsoft.com/netfx/2009/xaml

I read somewhere that using the /winfx/2006/xaml ns should just work but that doesn't seem to be the case.
neeraj
Commented on 8.February 2010
Is there a need of learning XAML..before i push my hand to a WPF application??
Or it can be learned side by side!!
JamesT
Commented on 10.March 2010
neeraj, wtf does "push my hand to a WPF application" mean?
Sergey
Commented on 19.May 2010
How can I use XAML 4 feature in regular WPF application? If I create workflow console application everything works, but not in WPF. Am I doing anything wrong?
Sergey
Commented on 19.May 2010
How can I use XAML 4 feature in regular WPF application? If I create workflow console application everything works, but not in WPF. Am I doing anything wrong?
Commented on 3.June 2010
Commented on 7.July 2010
Commented on 7.July 2010
Commented on 7.July 2010
Commented on 7.July 2010
Commented on 7.July 2010
Commented on 7.July 2010
Commented on 7.July 2010
Commented on 7.July 2010
Commented on 7.July 2010
Commented on 7.July 2010
Commented on 7.July 2010
Commented on 7.July 2010
Commented on 7.July 2010
Commented on 7.July 2010
Commented on 7.July 2010
Commented on 7.July 2010
Commented on 7.July 2010
Commented on 7.July 2010
Commented on 7.July 2010
Chatur
Commented on 7.July 2010
Blog likhnese kuch nahi hota he.. validation karna bhi ana chahiye... :)
Rajni Padhiyar
Commented on 12.July 2010
I like these new features,

Thanks
Rajni Padhiyar
rajnicby.si@gmail.com
shreyas
Commented on 28.July 2010
Nice man.......
Commented on 29.July 2010
Veni
Commented on 3.August 2010
Nice Ram.
bindu
Commented on 5.August 2010
hi very interesting features...but pls provide explanation with working examples apart from syntax..it will be more easy to understand for newbees like me...
Vimal
Commented on 13.August 2010
Hi
That is very nice features
Vimal
Commented on 13.August 2010
Hi
That is very nice features
this is nice...
Commented on 14.August 2010
explain with example
Mamta
Commented on 18.August 2010
This page is not detailed.
Jared
Commented on 5.September 2010
I like the conciseness here (not too many examples), which makes for a nice quick reference/overview.
sailaja
Commented on 18.September 2010
Hi the features you explained are nice;
but plz provide examples.
Frankly speaking i didnt understand full concept
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