What's new in XAML of .NET 4.0( .NET 4.0中XAML的新功能 )

原文 What's new in XAML of .NET 4.0

What's new in XAML of .NET 4.0

Easy Object References with {x:Reference}     使用{x:Reference}轻松引用对象

Built-in Types  内置类型

Generics in XAML with x:TypeArguments  XAML中的泛型 x:TypeArguments

Support for Arbitrary Dictionary Keys  支持任意字典键

Use of Non-Default Constructors with x:Arguments  使用非默认构造函数 x:Arguments

Use of Static Factory Methods with x:FactoryMethod  使用静态工厂方法 x:FactoryMethod

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

使用.NET 4.0,Microsoft将推出XAML的改进版本。这个arcitle向您展示了他们所做的语言增强。

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" />
 
上一篇:蓝牙中文API文档


下一篇:2019.01.21 NOIP训练 ak树(点分治)