Partager via


XAML 2009 Features: FactoryMethodArguments

This is part of a series on New WPF\XAML Features

Another new feature in XAML 2009 is the possibility of creating objects using the non-default constructors. The keyword here is x:Arguments

So creating a person object with a constructor requiring Name\Age could be written in XAML as

<local:Person>

    <x:Arguments>

        <x:String>Tom</x:String>

        <x:Int32>21</x:Int32> 

    </x:Arguments>

</local:Person>

 

Next interesting feature is using FactoryMethods… the keyword here is x:FactoryMethod

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

 

You could also combine x:Arguments with FactoryMethod

<coll:List x:Key='list' x:TypeArguments='x:String' x:FactoryMethod='local:Factory.CreateStringList'>

    <x:Arguments>

        <x:String>Mickey,Donald</x:String>

    </x:Arguments>

</coll:List>

 

Sample app showing usage is attached

Note that XAML 2009 features work only for loose XAML in WPF.

Share this post

 

Factory.zip