原文 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. android中checkbox自定义样式

    1.首先res/drawable中定义checkbox_style.xml样式: <?xml version="1.0" encoding="utf-8" ...

  2. encode()、decode()字符编码问题

    python3中,更改了默认的编码方式,更改为了unicode 前提知识: utf-8与Unicode的关系:https://baike.baidu.com/item/UTF-8/481798?fr= ...

  3. pycharm结合coding.net使用

    1,网上很多都是讲解pycharm结合github的,将pycharm的项目推送到github上. 现在很多博客都写了pycharm和github结合,其次,github同步时较慢,时而导致同步失败, ...

  4. 转:C# 深入理解堆栈、堆在内存中的实现

    尽管在.NET framework下我们并不需要担心内存管理和垃圾回收(GarbageCollection),但是我们还是应该了解它们,以优化我们的应用程序.同时,还需要具备一些基础的内存管理工作机制 ...

  5. django —— 邮件

    官方文档 1.11 配置settings.py # QQ邮箱为例, 其他邮箱对应的SMTP配置可查官方 EMAIL_HOST = "smtp.qq.com" EMAIL_PORT ...

  6. Git如何永久删除文件(包括历史记录)

    有些时候不小心上传了一些敏感文件(例如密码), 或者不想上传的文件(没及时或忘了加到.gitignore里的), 而且上传的文件又特别大的时候, 这将导致别人clone你的代码或下载zip包的时候也必 ...

  7. JavaScript原型链基础(prototype chain)

    1.函数基础 2.对象基础 3.原型链基础

  8. skype客户端搜不到联系人解决办法

    1.确认skype客户端登陆的配置信息 按住Ctrl 键不放,右击右下角任务栏 skype 图标,选择:配置信息,确认skype的:GAL搜索或基于服务器的搜索为:基于服务器的搜索,如下图所示: 2. ...

  9. 处理AsyncTask的内存泄漏问题

    强引用AsyncTask导致了内存泄漏如下图 1.原因:activity销毁之后,AsyncTask线程可能依旧在执行,导致内存泄漏. 2.解决方法:查了一下大概有两个,一个是将函数声明为static ...

  10. 极限编程核心价值:简单(Simplicity)

    写在前面 在编写 ASP.NET Core 项目时,深感项目设计的无力感,在软件设计方面我还有很长的路要走.我一直以来都把代码当作一种艺术的存在,认为自己是个"艺术家",其实就是个 ...