x:Type 标记扩展x:Type Markup Extension

提供作为 Type 指定 XAML 类型的基础类型的 CLR 对象。Supplies the CLR Type object that is the underlying type for a specified XAML type.

XAML 属性用法XAML Attribute Usage

<object property="{x:Type prefix:typeNameValue}" .../>

XAML 对象元素用法XAML Object Element Usage

<x:Type TypeName="prefix:typeNameValue"/>

XAML 值XAML Values

prefix 可选。Optional. 映射非默认 XAML 命名空间的前缀。A prefix that maps a non-default XAML namespace. 通常不需要指定前缀。Specifying a prefix is frequently not necessary. 请参阅“备注”。See Remarks.
typeNameValue 必需。Required. 可解析为当前默认 XAML 命名空间的类型名称;如果提供,则为指定的映射前缀 prefixA type name resolvable to the current default XAML namespace; or the specified mapped prefix if prefix is supplied.

备注Remarks

x:Type标记扩展的函数与 typeof() c # 中的运算符或 GetType Microsoft Visual Basic 中的运算符类似。The x:Type markup extension has a similar function to the typeof() operator in C# or the GetType operator in Microsoft Visual Basic.

x:Type标记扩展为采用该类型的属性提供 from 字符串转换行为 TypeThe x:Type markup extension supplies a from-string conversion behavior for properties that take the type Type. 输入为 XAML 类型。The input is a XAML type. 输入 XAML 类型和输出 CLR 之间的关系 TypeType UnderlyingType XamlTypeXamlType 在根据 XAML 架构上下文和上下文提供的服务查找必需的后,输出为输入的 IXamlTypeResolverThe relationship between the input XAML type and the output CLR Type is that the output Type is the UnderlyingType of the input XamlType, after looking up the necessary XamlType based on XAML schema context and the IXamlTypeResolver service the context provides.

在 .NET XAML 服务中,对此标记扩展的处理由类定义 TypeExtensionIn .NET XAML Services, the handling for this markup extension is defined by the TypeExtension class.

在特定的框架实现中,一些 Type 作为值使用的属性可以直接接受 (类型) 的字符串值的类型名称 NameIn specific framework implementations, some properties that take Type as a value can accept the name of the type directly (the string value of the type Name). 但是,实现此行为是一种复杂的方案。However, implementing this behavior is a complex scenario. 有关示例,请参阅后面的 "WPF 用法说明" 部分。For examples, see the "WPF Usage Notes" section that follows.

特性语法是最常用于该标记扩展的语法。Attribute syntax is the most common syntax used with this markup extension. x:Type 标识符字符串之后提供的字符串标记被指定为基础 TypeName 扩展类的 TypeExtension 值。The string token provided after the x:Type identifier string is assigned as the TypeName value of the underlying TypeExtension extension class. 在基于 CLR 类型的 .NET XAML 服务的默认 XAML 架构上下文下,此特性的值是 Name 所需类型的,或者包含 Name 前面带有非默认 XAML 命名空间映射前缀的。Under the default XAML schema context for .NET XAML Services, which is based on CLR types, the value of this attribute is either the Name of the desired type, or contains that Name preceded by a prefix for a non-default XAML namespace mapping.

x:Type标记扩展可用于对象元素语法。The x:Type markup extension can be used in object element syntax. 在这种情况下,需要指定属性的值 TypeName 才能正确地初始化扩展。In this case, specifying the value of the TypeName property is required to properly initialize the extension.

x:Type标记扩展还可用作详细特性; 但这种用法并不典型:<object property="{x:Type TypeName=typeNameValue}" .../>The x:Type markup extension can also be used as a verbose attribute; however this use is not typical: <object property="{x:Type TypeName=typeNameValue}" .../>

WPF 用法说明WPF Usage Notes

默认 XAML 命名空间和类型映射Default XAML Namespace and Type Mapping

WPF 编程的默认 XAML 命名空间包含典型 XAML 方案所需的大多数 XAML 类型;因此,在引用 XAML 类型值时,通常可以避免前缀。The default XAML namespace for WPF programming contains most of the XAML types you need for typical XAML scenarios; therefore, you can often avoid prefixes when referencing XAML type values. 如果引用的是自定义程序集的类型或存在于 WPF 程序集中的类型,但却来自未映射到默认 XAML 命名空间的 CLR 命名空间,则可能需要映射前缀。You might need to map a prefix if you are referencing a type from a custom assembly or for types that exist in a WPF assembly but are from a CLR namespace that was not mapped to the default XAML namespace. 有关前缀、XAML 命名空间和映射 CLR 命名空间的详细信息,请参阅 WPF xaml 的 XAML 命名空间和命名空间映射For more information about prefixes, XAML namespaces, and mapping CLR namespaces, see XAML Namespaces and Namespace Mapping for WPF XAML.

支持类型为字符串的类型属性Type Properties That Support Typename-as-String

WPF 支持用于指定类型的某些属性的值的方法, Type 无需 x:Type 使用标记扩展用法。WPF supports techniques that enable specifying the value of some properties of type Type without requiring an x:Type markup extension usage. 相反,你可以将值指定为命名类型的字符串。Instead, you can specify the value as a string that names the type. 这是和的 ControlTemplate.TargetType 示例 Style.TargetTypeExamples of this are ControlTemplate.TargetType and Style.TargetType. 不通过类型转换器或标记扩展提供对此行为的支持。Support for this behavior is not provided through either type converters or markup extensions. 相反,这是通过实现的延迟行为 FrameworkElementFactoryInstead, this is a deferral behavior implemented through FrameworkElementFactory.

Silverlight 支持类似的约定。Silverlight supports a similar convention. 事实上,Silverlight 目前不支持 {x:Type} 其 XAML 语言支持,并且不接受 {x:Type} 旨在支持 WPF-Silverlight XAML 迁移的少数情况下的用法。In fact, Silverlight does not currently support {x:Type} in its XAML language support, and does not accept {x:Type} usages outside of a few circumstances that are intended to support WPF-Silverlight XAML migration. 因此,类型即字符串行为内置于所有 Silverlight 本机属性评估中,其中 a Type 是值。Therefore, the typename-as-string behavior is built-in to all Silverlight native property evaluation where a Type is the value.

XAML 2009XAML 2009

XAML 2009 提供对泛型类型的其他支持,并修改和的功能行为 x:TypeArguments x:Type 以提供此支持。XAML 2009 provides additional support for generic types and modifies the feature behavior of x:TypeArguments and x:Type to provide this support.

  • x:TypeArguments 与泛型对象实例化关联的对象元素可以位于根以外的元素上。x:TypeArguments and the associated object element for a generic object instantiation can be on elements other than the root. 有关详细信息,请参阅 X:TypeArguments 指令的 "XAML 2009" 一节。For more information, see the "XAML 2009" section of x:TypeArguments Directive.

  • XAML 2009 支持用于在标记中指定泛型类型的约束的语法。XAML 2009 supports a syntax for specifying a generic type's constraint in markup. 这可以由 x:TypeArguments 、通过 x:Type 或两个功能组合使用。This can be used by x:TypeArguments, by x:Type, or by the two features in combination.

  • 当处理 XAML 2009 for load 时,WPF XAML 实现还会将此功能添加到使用类型的某些框架属性的隐式类型转换行为 TypeWPF XAML implementation when processing XAML 2009 for load also adds this capability to the implicit type conversion behavior for certain framework properties that use type Type.

在 WPF 中,可以使用 XAML 2009 功能,但仅针对未进行标记编译) 的松散 XAML (XAML。In WPF, you can use XAML 2009 features but only for loose XAML (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.

另请参阅See also