代码改变世界

xaml2009新特性

2009-12-03 16:05  Clingingboy  阅读(872)  评论(1编辑  收藏  举报
New WPF Features

http://blogs.msdn.com/llobo/archive/2009/11/25/concluding-new-wpf-features-series.aspx

   基本的特性,大家参考一下,若有遗漏,还请补充

一.支持CLR基本类型

 

http://schemas.microsoft.com/winfx/2006/xaml命名空间默认支持CLR基本类型


之前要在xaml中使用基本类型,必须引用其命名空间
  .net4.0之前

<s:String xmlns:s="clr-namespace:System;assembly=mscorlib" >Foo</s:String>



.net4.0之后,无需再引用命名空间了,进行了简化

<x:String>Foo</s:String>

 

基本类型包括

• <x:String>

• <x:Char>

• <x:Single>

• <x:Double>

• <x:Boolean>

• <x:Byte>

• <x:Int16>

• <x:Int32>

• <x:Int64>

• <x:Decimal>

• <x:Object>

• <x:Uri>

• <x:TimeSpan>

• <x:Array>

http://blogs.msdn.com/llobo/archive/2009/11/05/xaml-2009-features-built-in-types.aspx

二.泛型支持(x:TypeArguments)


要在xaml中定义动态泛型对象

       <ObservableCollection x:TypeArguments='local:Person' 
                             xmlns='clr-namespace:System.Collections.ObjectModel;assembly=System'  >
            <local:Person Name='Tom' Age='21' />
        </ObservableCollection>


 
http://blogs.msdn.com/llobo/archive/2009/11/19/xaml-2009-features-generics-support.aspx

三.非默认构造函数创建对象((x:Arguments)


之前在xaml中的对象,只能是默认构造函数,灵活性降低,现在

        <local:Person>
            <x:Arguments>
                <x:String>Tom</x:String>
                <x:Int32>21</x:Int32>
            </x:Arguments>
        </local:Person>

 

四.支持静态方法创建对象


如创建一个新的Guid,之间是无法做到的,现在
(1)对象自身静态方法

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


(2)其他对象静态方法,可传方法参数

    <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>

 

http://blogs.msdn.com/llobo/archive/2009/11/20/xaml-2009-features-factorymethod-arguments.aspx

五.已命名对象引用(x:Reference)


之前绑定写法

    <Label Target='{Binding ElementName=firstNameBox}' >_Target</Label>
    <TextBox Name='firstNameBox'>Uses Binding</TextBox>

 

现在

<Label Target= '{x:Reference secondNameBox}'>_Second Target</Label>

 

六.自定义标记扩展

这在之前版本就已经支持了,重在简化开发,差不多就是三行代码可以并成一行代码,不过不是内置的api,大家都懒的去学,有时候没必要,属于自己做着玩的,不过有时候也是必要的,可以关注一下

http://www.cnblogs.com/Clingingboy/archive/2009/02/02/1382444.html

http://blogs.msdn.com/llobo/archive/2009/11/11/xaml-2009-features-xaml-event-handling.aspx

七.动态解析xaml

上面说过,vs2010 beta2还不支持编译以上特性,可以通过动态解析来实现上面功能

http://blogs.msdn.com/llobo/archive/2009/11/09/xaml-2009-features-node-loop-flexibility.aspx

 

.net 4.0版本之后,最大的变化在于将xaml独立了出来(System.Xaml),以上特性只是对xaml进行稍微的增强,其他的可以通过自定义标记扩展来实现.