Anup Shah on WPF and Silverlight (Programming Garden)

IT 's For You!!!

Friday, December 30, 2011

What's new in XAML of .NET 4.0

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



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.

   xmlns:sys="clr-namespace:System;assembly=mscorlib >Test    Test    

The following types are included into the XAML language:

Generics in XAML with x:TypeArguments

If you want to use an ObservableCollection in XAML you need to create a type that derives fromObservableCollection because you cannot declare it in XAML. With XAML 2009 you can use the x:TypeArgumentsattribute to define the type of a generic type.

   class EmployeeCollection : ObservableCollection { }        FirstName="John" Name="Doe" />     FirstName="Tim" Name="Smith" />     x:TypeArguments="Employee">     FirstName="John" Name="Doe" />     FirstName="Tim" Name="Smith" />     

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.

   x:Key="CheckGeometry">M 0 0 L 12 8 l 9 12 z    M 0 0 L 12 8 l 9 12 z     10.0     

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.

   00:00:00.0000100                  100          

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.

   Guid id = Guid.NewGuid();    x:FactoryMethod="Guid.NewGuid" />    

No comments:

Post a Comment