Monday, May 2, 2011

DotNet 4.0 - New Features introduced in XAML 2009



New XAML with WPF 4.0 called XAML 2009. In XAML 2009, Microsoft added lots of new features but unfortunately this features not supported by Visual Studio 2010. If you want to use new features of XAML 2009, you have to use loose XAML file or uncompiled XAML file. You can dynamically load loose XAML content in window and use all the new features available in XAML 2009. In below examples i have used loose XAML content.

I am going to explain new features introduced in XAML 2009 here.

  • References
  • Built in Types
  • Generic Support
  • Advance Object Creation
  • Instance via Factory Methods
  • Arbitrary Dictionary Key

References

You can create references to the other element. Use x:Reference markup extension on Target property of Label to give reference of other element.

<StackPanel Orientation='Horizontal'>
<Label Target='{x:Reference NameTextBox}' >Enter _Name :</Label>
       <TextBox x:Name='NameTextBox' Width='150' > </TextBox>
</StackPanel>

As per above example, When user press ALT +N key the NameTextBox will get focus on it.

Built in Types

With XAML 2009, we can access commonly used type using ‘x’ namespace. It includes String, Int32, Int64, Decimal, Single, Array etc.

<x:String>WPF</x:String>

Generic Support

You can use generic collection in XAML. If you want to use ObservableCollection<T> in your XAML you can create it like this in XAML. You can use x:TypeArguments to specify Generic Type.

<ObservableCollection x:Key='collection' x:TypeArguments='local:Employee'
  xmlns='clr-namespace:System.Collections.ObjectModel;assembly=System'>
    <local:Employee>
        <x:Arguments>
            <x:String>Mitesh</x:String>
            <x:Int32>29</x:Int32>
        </x:Arguments>
    </local:Employee>
    <local:Employee>
        <x:Arguments>
            <x:String>Pritesh</x:String>
            <x:Int32>30</x:Int32>
         </x:Arguments>
    </local:Employee>
</ObservableCollection>

Similar way you can use generic list (e.g. List<string>) also.

Advance Object Creation

You can create an instance without non default constructors. You can specify one or more arguments to constructor by using x:Arguments.

XMAL:
<local:Employee>
<x:Arguments>
              <x:String>Mitesh</x:String>
              <x:Int32>29</x:Int32>
       </x:Arguments>
</local:Employee>
Code:
 public class Employee
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public Employee(string name, int age)
        {
            Name = name;
            Age = age;
        }
    }

Instance via Factory Methods

You can create an instance using Factory Methods in XAML. Using x:FactoryMethod you can specify any public static method and get an instance of your type. You can also use x:Arguments to pass parameters to static method.

<sys:Guid x:FactoryMethod='NewGuid' />

This is similar to call Guid id =Guid.NewGuid() in codebehind. As per above example, You can access NewGuid static method in XAML.

Arbitrary Dictionary Key

You can use x:Key as an object in XAML 2009.  In earlier version of XAML you can add x:Key as an attribute but with XAML 2009 you can add x:Key as an object.

<sys:Guid x:FactoryMethod='NewGuid'>
<x:Key>Uid</x:Key>
</sys:Guid>



Read more this here

2 comments: