常见 XAML 语言基元的内置类型Built-in types for common XAML language primitives

XAML 2009 引入了对多种数据类型的 XAML 语言级支持(这些数据类型是公共语言运行时 (CLR) 和其他编程语言中的常用基元)。XAML 2009 introduces XAML language-level support for several data types that are frequently used primitives in the common language runtime (CLR) and in other programming languages. XAML 2009 增加了对以下基元的支持: x:Objectx:Booleanx:Charx:Stringx:Decimalx:Singlex:Doublex:Int16x:Int32x:Int64x:TimeSpanx:Urix:Bytex:ArrayXAML 2009 adds support for these primitives: 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, and x:Array

用于 XAML 标记中语言基元的早期方法Previous Techniques for Language Primitives in XAML Markup

在早期 WPF 版本的 XAML 中,可以通过映射程序集以及包含 .NET Framework 的 CLR 基元定义类的命名空间来引用 CLR 语言基元。In XAML for previous WPF versions, you could reference the CLR language primitives by mapping the assembly and namespace that contained a CLR primitive definition class for the .NET Framework. 它们中的大部分都位于 mscorlib 程序集和 System 命名空间中。Most of these are in the mscorlib assembly and System namespace. 例如,要使用 Int32,可以声明以下映射(用法示例如后面所示):For example, to use Int32, you could declare the following mapping (with an example usage shown thereafter):

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
  <Application.Resources>
    <sys:Int32 x:Key="intMeaning">42</sys:Int32>
  </Application.Resources>
</Application>

XAML 2009 语言基元XAML 2009 Language Primitives

按照约定,将显示 XAML 的语言基元以及所有其他 XAML 语言元素,包括 x: 前缀。By convention, the language primitives for XAML and all other XAML language elements are shown, including the x: prefix. 这是 XAML 语言元素在实际标记中的典型用法。This is how XAML language elements are typically used in real-world markup. WPF 中 XAML 的概念性文档以及 XAML 规范遵从此约定。This convention is followed in the conceptual documentation for XAML in WPF and also in the XAML specification.

x:Objectx:Object

对于 CLR 支持, x:Object 基元对应于 ObjectFor CLR backing, the x:Object primitive corresponds to Object.

此基元通常不在应用程序标记中使用,但对于某些情况(如检查 XAML 类型系统中的可分配性)可能非常有用。This primitive is not typically used in application markup, but might be useful for some scenarios such as checking assignability in a XAML type system.

x:Booleanx:Boolean

对于 CLR 支持, x:Boolean 基元对应于 BooleanFor CLR backing, the x:Boolean primitive corresponds to Boolean.

XAML 分析 x:Boolean 的值时不区分大小写。XAML parses values for x:Boolean as case insensitive. 请注意, x:Bool 不是可接受的备选基元。Note that x:Bool is not an accepted alternative. 有关 XAML 语言规范定义,请参阅[] MS-XAML 第 5.2.17 节和 5.4.11节 。For the XAML language specification definition, see [MS-XAML] Sections 5.2.17 and 5.4.11.

x:Charx:Char

对于 CLR 支持, x:Char 基元对应于 CharFor CLR backing, the x:Char primitive corresponds to Char.

String 和 char 类型在 XML 级别与文件的整个编码进行交互。String and char types have interaction with the overall encoding of the file at the XML level. 有关 XAML 语言规范定义,请参阅[] MS-XAML 第 5.2.7 节和 5.4.1节 。For the XAML language specification definition, see [MS-XAML] Sections 5.2.7 and 5.4.1.

x:Stringx:String

对于 CLR 支持, x:String 基元对应于 StringFor CLR backing, the x:String primitive corresponds to String.

String 和 char 类型在 XML 级别与文件的整个编码进行交互。String and char types have interaction with the overall encoding of the file at the XML level. 有关 XAML 语言规范定义,请参阅[MS-XAML]第 5.2.6 节For the XAML language specification definition, see [MS-XAML] Sections 5.2.6.

x:Decimalx:Decimal

对于 CLR 支持, x:Decimal 基元对应于 DecimalFor CLR backing, the x:Decimal primitive corresponds to Decimal.

XAML 解析本质上是在区域性下en-US完成的。XAML parsing is inherently done under en-US culture. en-US 区域性设置下,小数部分的正确分隔符始终为句点 (.),而与开发环境的区域性设置或在运行时加载 XAML 的最终客户端目标的区域性设置无关。Under en-US culture, the correct separator for the components of a decimal is always a period (.) regardless of culture settings of the development environment, or of the eventual client target where the XAML is loaded at run time.

有关 XAML 语言规范定义,请参阅[] MS-XAML 第 5.2.14 节和 5.4.8节 。For the XAML language specification definition, see [MS-XAML] Sections 5.2.14 and 5.4.8.

x:Singlex:Single

对于 CLR 支持, x:Single 基元对应于 SingleFor CLR backing, the x:Single primitive corresponds to Single.

除了数值之外, x:Single 的文本语法还允许使用标记 Infinity-InfinityNaNIn addition to the numeric values, text syntax for x:Single also permits the tokens Infinity, -Infinity, and NaN. 这些标记被视为区分大小写。These tokens are treated as case sensitive.

x:Single 支持采用科学记数法格式的值,条件是文本语法中的第一个字符为 eEx:Single can support values in scientific notation form, if the first character in text syntax is e or E.

有关 XAML 语言规范定义,请参阅[] MS-XAML 第 5.2.8 节和 5.4.2节 。For the XAML language specification definition, see [MS-XAML] Sections 5.2.8 and 5.4.2.

x:Doublex:Double

对于 CLR 支持, x:Double 基元对应于 DoubleFor CLR backing, the x:Double primitive corresponds to Double.

除了数值之外, x:Double 的文本语法还允许使用标记 Infinity-InfinityNaNIn addition to the numeric values, text syntax for x:Double permits the tokens Infinity, -Infinity, and NaN. 这些标记被视为区分大小写。These tokens are treated as case sensitive.

x:Double 支持采用科学记数法格式的值。x:Double can support values in scientific notation form. 使用字符 eE 可引入指数部分。Use the character e or E to introduce the exponent portion.

有关 XAML 语言规范定义,请参阅[] MS-XAML 第 5.2.9 节和 5.4.3节 。For the XAML language specification definition, see [MS-XAML] Sections 5.2.9 and 5.4.3.

x:Int16x:Int16

对于 CLR 支持, x:Int16 基元对应于 Int16 ,并且 x:Int16 被视为带符号。For CLR backing, the x:Int16 primitive corresponds to Int16 and x:Int16 is treated as signed. 在 XAML 中,文本语法中没有加号 (+) 表示是一个带符号的正值。In XAML, the absence of a plus (+) sign in text syntax is implied as a positive signed value.

有关 XAML 语言规范定义,请参阅[] MS-XAML 第 5.2.11 节和 5.4.5节 。For the XAML language specification definition, see [MS-XAML] Sections 5.2.11 and 5.4.5.

x:Int32x:Int32

对于 CLR 支持, x:Int32 基元对应于 Int32For CLR backing, the x:Int32 primitive corresponds to Int32. x:Int32 被视为带符号。x:Int32 is treated as signed. 在 XAML 中,文本语法中没有加号 (+) 表示是一个带符号的正值。In XAML, the absence of a plus (+) sign in text syntax is implied as a positive signed value.

有关 XAML 语言规范定义,请参阅[] MS-XAML 第 5.2.12 节和 5.4.6节 。For the XAML language specification definition, see [MS-XAML] Sections 5.2.12 and 5.4.6.

x:Int64x:Int64

对于 CLR 支持, x:Int64 基元对应于 Int64For CLR backing, the x:Int64 primitive corresponds to Int64. x:Int64 被视为带符号。x:Int64 is treated as signed. 在 XAML 中,文本语法中没有加号 (+) 表示是一个带符号的正值。In XAML, the absence of a plus (+) sign in text syntax is implied as a positive signed value.

有关 XAML 语言规范定义,请参阅[] MS-XAML 第 5.2.13 节和 5.4.7节 。For the XAML language specification definition, see [MS-XAML] Sections 5.2.13 and 5.4.7.

x:TimeSpanx:TimeSpan

对于 CLR 支持, x:TimeSpan 基元对应于 TimeSpanFor CLR backing, the x:TimeSpan primitive corresponds to TimeSpan.

时间日期格式的 XAML 解析本质上是在区域性下en-US完成的。XAML parsing for time-date format is inherently done under en-US culture.

有关 XAML 语言规范定义,请参阅[] MS-XAML 第 5.2.16 节和 5.4.10节 。For the XAML language specification definition, see [MS-XAML] Sections 5.2.16 and 5.4.10.

x:Urix:Uri

对于 CLR 支持, x:Uri 基元对应于 UriFor CLR backing, the x:Uri primitive corresponds to Uri.

检查协议不是 x:Uri的 XAML 定义的一部分。Checking for protocols is not part of the XAML definition for x:Uri.

有关 XAML 语言规范定义,请参阅[] MS-XAML 第 5.2.15 节和 5.4.9节 。For the XAML language specification definition, see [MS-XAML] Sections 5.2.15 and 5.4.9.

x:Bytex:Byte

对于 CLR 支持, x:Byte 基元对应于 ByteFor CLR backing, the x:Byte primitive corresponds to Byte. Byte / Ax:Byte被视为未签名。A Byte / x:Byte is treated as unsigned.

有关 XAML 语言规范定义,请参阅[] MS-XAML 第 5.2.10 节和 5.4.4节 。For the XAML language specification definition, see [MS-XAML] Sections 5.2.10 and 5.4.4.

x:Arrayx:Array

对于 CLR 支持, x:Array 基元对应于 ArrayFor CLR backing, the x:Array primitive corresponds to Array.

可以在 XAML 2006 中使用标记扩展语法定义一个数组;而 XAML 2009 语法是语言定义的基元,不需要访问标记扩展。You can define an array in XAML 2006 by using a markup extension syntax; however, the XAML 2009 syntax is a language-defined primitive that does not require accessing a markup extension. 有关 XAML 2006 支持的更多信息,请参阅 x:Array Markup ExtensionFor more information about XAML 2006 support, see x:Array Markup Extension.

有关 XAML 语言规范定义,请参阅[MS-XAML]第 5.2.18 节For the XAML language specification definition, see [MS-XAML] Sections 5.2.18.

WPF 支持WPF Support

在 WPF 中,可以使用 XAML 2009 功能,但仅针对未进行标记编译的 XAML。In WPF, you can use XAML 2009 features but only for XAML that is not markup-compiled. WPF 的已编译标记的 XAML 以及 XAML 的 BAML 形式当前不支持 XAML 2009 关键字和功能。Markup-compiled XAML for WPF and the BAML form of XAML do not currently support the XAML 2009 keywords and features.

可使用 XAML 2009 功能以及 WPF 的一种情况是创作宽松 XAML,然后使用 XamlReader.Load将该 XAML 加载到 WPF 运行时和对象图中。A scenario where you can use XAML 2009 features together with WPF is if you author loose XAML and you then load that XAML into a WPF runtime and object graph with XamlReader.Load. WPF System.Windows.Markup.XamlReader 及其 Load 可以将 XAML 2009 语言关键字和功能处理成一个有效的对象图表示形式。The WPF System.Windows.Markup.XamlReader and its Load can process XAML 2009 language keywords and features into a valid object graph representation.