原文 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" />
 

What's new in XAML of .NET 4.0( .NET 4.0中XAML的新功能 )的更多相关文章

  1. 捕捉WPF应用程序中XAML代码解析异常

    原文:捕捉WPF应用程序中XAML代码解析异常 由于WPF应用程序中XAML代码在很多时候是运行时加载处理的.比如DynamicResource,但是在编译或者运行的过程中,编写的XAML代码很可能有 ...

  2. 关于 WP 开发中.xaml 与.xaml.cs 的关系

    今天我们先来看一下在WP8.1开发中最长见到的几个文件之间的关系.比较论证,在看这个问题之前我们简单看看.NET平台其他两个不同的框架: Windows Forms 先看看Window Forms中的 ...

  3. WP 开发中.xaml 与.xaml.cs

    关于 WP 开发中.xaml 与.xaml.cs 的关系   今天我们先来看一下在WP8.1开发中最长见到的几个文件之间的关系.比较论证,在看这个问题之前我们简单看看.NET平台其他两个不同的框架: ...

  4. wpf中xaml的类型转换器与标记扩展

    原文:wpf中xaml的类型转换器与标记扩展 这篇来讲wpf控件属性的类型转换器 类型转换器 类型转换器在asp.net控件中已经有使用过了,由于wpf的界面是可以由xaml组成的,所以标签的便利也需 ...

  5. 整理:WPF中Xaml中绑定枚举的写法

    原文:整理:WPF中Xaml中绑定枚举的写法 目的:在Combobox.ListBox中直接绑定枚举对象的方式,比如:直接绑定字体类型.所有颜色等枚举类型非常方便 一.首先用ObjectDataPro ...

  6. 【随记】WPF中xaml输入中文乱码问题解决

    在Visual Studio中开发WPF应用程序时,在XMAL文档编写界面输入中文时变为乱码.可能的原因之一是VS中安装了VAssistX插件,导致编码冲突,使中文输入乱码.解决方法是在VAssist ...

  7. WPF中XAML的触发器的属性,事件 都有那些?以及如何寻找

    在编写XAML的触发器时,会有属性,事件. 那么这些都是哪里来的呢? 属性,是附加属性或者依赖属性 事件,那就是事件. 如何寻找呢? 很简单,在想要使用的触发器的对象上(也就是有光标的时候)按下F12 ...

  8. WPF中Xaml编译正常而Designer Time时出错的解决办法

    开发wpf时我们经常遇到一个xaml文件在设计时显示解析错误(比如在:VS或者Blend)而编译正常运行正常. 原因是:xaml的在Debug版本下必须为anyCPU. 解决办法: 1.打开工程文件x ...

  9. WPF中XAML转义字符

    字符 转义字符 备注 & (ampersand) & 这个没什么特别的,几乎所有的地方都需要使用转义字符 > (greater-than character) > 在属性( ...

随机推荐

  1. 【LLVM笔记】0x00 初识LLVM 链接类型

    模块结构 LLVM程序是由若干的模块(Module)组成,每个模块中包含有一些函数.全局变量和符号表. 这些模块可能由LLVM的连接器组合在一起,组合的过程将会整合这些函数和全局变量的定义,整合他们的 ...

  2. mysql 建立索引的原则(转)

    索引查询是数据库中重要的记录查询方法,要不要进入索引以及在那些字段上建立索引都要和实际数据库系统的查询要求结合来考虑,下面给出实际中的一些通用的原则: 1. 在经常用作过滤器的字段上建立索引: 2. ...

  3. Oracle EBS AR 其他API

    DECLARE L_CR_ID NUMBER; L_ATTRIBUTE_REC AR_RECEIPT_API_PUB.ATTRIBUTE_REC_TYPE; L_GLOBAL_ATT_REC AR_R ...

  4. SQL Server中ORDER BY后面可以是表达式和子查询

    假如SQL Server数据库中现在有Book表如下 CREATE TABLE [dbo].[Book]( ,) NOT NULL, ) NULL, ) NULL, ) NULL, [CreateTi ...

  5. 总结Linux 下Redis 操作常用命令(转)

    Redis的配置 Linux下安装 ]# wget http://download.redis.io/releases/redis-2.8.17.tar.gz ]# tar xzf redis-2.8 ...

  6. MySQL索引与Index Condition Pushdown(employees示例)

    实验 先从一个简单的实验开始直观认识ICP的作用. 安装数据库 首先需要安装一个支持ICP的MariaDB或MySQL数据库.我使用的是MariaDB 5.5.34,如果是使用MySQL则需要5.6版 ...

  7. AD用户移除所属组

    AD用户移除所属组: $Membership = Get-ADPrincipalGroupMembership $Users $Membership.distinguishedName Remove- ...

  8. PHP 传值操作和传地址操作

    PHP中有两种赋值操作 一种是传值操作 example: $a = 1; $b = $b; 这个就是传值 传地址是: example2: $a = 1; $b = &$a; 这两者有什么区别呢 ...

  9. 另开一篇 https

    https 流程 1.加密传输:对称加密传输信息 2.身份认证:非对称加密.通过证书来保障客户端给服务器的密钥唯一性. 因为中间层要是伪装公钥和证书,但是又无法解密原有的发送的数据,那么发给服务器的数 ...

  10. 深入学习css之background属性

    css中允许应用纯色作为背景,也允许使用图片作为背景. background一共有8个对应的属性: 1.background-color:颜色值 用于设定背景的颜色 有3种定义颜色的形式, 1, 颜色 ...