XAML 命名空间和命名空间映射XAML namespaces and namespace mapping

本主题介绍大多数 XAML 文件的根元素中用到的 XML/XAML 命名空间 (xmlns) 映射。This topic explains the XML/XAML namespace (xmlns) mappings as found in the root element of most XAML files. 它还将介绍如何为自定义类型和程序集生成类似的映射。It also describes how to produce similar mappings for custom types and assemblies.

XAML 命名空间如何与代码定义和类型库相关How XAML namespaces relate to code definition and type libraries

无论是其一般用途还是 Windows 运行时应用编程的应用上,XAML 都用于声明对象、这些对象的属性以及表示为层次结构的对象-属性关系。Both in its general purpose and for its application to Windows Runtime app programming, XAML is used to declare objects, properties of those objects, and object-property relationships expressed as hierarchies. 你在 XAML 中声明的对象受其他编程技术和语言定义的类型库或其他表示支持。The objects you declare in XAML are backed by type libraries or other representations that are defined by other programming techniques and languages. 这些库可能是:These libraries might be:

  • Windows 运行时的内置对象集合。The built-in set of objects for the Windows Runtime. 这是一个固定的对象集合,从 XAML 访问这些对象使用内部类型映射和激活逻辑。This is a fixed set of objects, and accessing these objects from XAML uses internal type-mapping and activation logic.
  • Microsoft 或第三方提供的分布式库。Distributed libraries that are provided either by Microsoft or by third parties.
  • 该库表示你的应用包含的以及你的包重新分发的第三方控件的定义。Libraries that represent the definition of a third-party control that your app incorporates and your package redistributes.
  • 你自己的库(属于你的项目的一部分),它包含部分或所有用户代码定义。Your own library, which is part of your project and which holds some or all of your user code definitions.

支持类型信息与特定 XAML 命名空间定义相关联。Backing type info is associated with particular XAML namespace definitions. XAML 框架(如 Windows 运行时)可聚合多个程序集和多个代码命名空间,以映射到单个 XAML 命名空间。XAML frameworks such as the Windows Runtime can aggregate multiple assemblies and multiple code namespaces to map to a single XAML namespace. 这就支持涉及更大的编程框架或技术的 XAML 词汇表概念。This enables the concept of a XAML vocabulary that covers a larger programming framework or technology. XAML 词汇表可能非常大,例如该引用中为 Windows 运行时应用记录的大部分 XAML 构成单个 XAML 词汇表。A XAML vocabulary can be quite extensive—for example, most of the XAML documented for Windows Runtime apps in this reference constitutes a single XAML vocabulary. XAML 词汇表也可扩展:通过向支持代码定义添加类型来扩展它,从而确保在代码命名空间(已经用作 XAML 词汇表的映射的命名空间来源)中包含这些类型。A XAML vocabulary is also extensible: you extend it by adding types to the backing code definitions, making sure to include the types in code namespaces that are already used as mapped namespace sources for the XAML vocabulary.

XAML 处理器在创建运行时对象表示时,可查找与该 XAML 命名空间关联的支持程序集的类型和成员。A XAML processor can look up types and members from the backing assemblies associated with that XAML namespace when it creates a run-time object representation. 出于此原因,XAML 可用作一种形式化和交换对象构造定义行为的方式,并且 XAML 可用作 UWP 应用的 UI 定义技术。This is why XAML is useful as a way to formalize and exchange definitions of object-construction behavior, and why XAML is used as a UI definition technique for a UWP app.

典型 XAML 标记中使用的 XAML 命名空间XAML namespaces in typical XAML markup usage

XAML 文件几乎总是在其根元素中声明一个默认 XAML 命名空间。A XAML file almost always declares a default XAML namespace in its root element. 默认 XAML 命名空间定义了无需使用前缀来限定即可声明哪些元素。The default XAML namespace defines which elements you can declare without qualifying them by a prefix. 例如,如果声明一个元素 <Balloon />,XAML 分析器期望一个 Balloon 元素存在并且在默认的 XAML 命名空间中是有效的。For example, if you declare an element <Balloon />, a XAML parser will expect that an element Balloon exists and is valid in the default XAML namespace. 相反,如果 Balloon 不在已定义的默认 XAML 命名空间中,就必须使用一个前缀限定该元素名称,例如 <party:Balloon />In contrast, if Balloon is not in the defined default XAML namespace, you must instead qualify that element name with a prefix, for example <party:Balloon />. 该前缀表明该元素存在于与默认命名空间不同的 XAML 命名空间中,必须将一个 XAML 命名空间映射到前缀 party,然后才能使用此元素。The prefix indicates that the element exists in a different XAML namespace than the default namespace, and you must map a XAML namespace to the prefix party before you can use this element. XAML 命名空间适用于在其中声明它们的特定元素,也适用于该元素在 XAML 结构中包含的任何元素。XAML namespaces apply to the specific element on which they are declared, and also to any element that is contained by that element in the XAML structure. 出于此原因,XAML 命名空间几乎总是在 XAML 文件的根元素上声明,以充分利用这种继承性。For this reason, XAML namespaces are almost always declared on root elements of a XAML file to take advantage of this inheritance.

默认和 XAML 语言 XAML 命名空间声明The default and XAML language XAML namespace declarations

在大多数 XAML 文件的根元素中,有两个 xmlns 声明。Within the root element of most XAML files, there are two xmlns declarations. 第一个声明将 XAML 命名空间映射为默认值: xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"The first declaration maps a XAML namespace as the default: xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

这是多个也使用 XAML 作为 UI 定义标记格式的预处理器 Microsoft 技术中使用的相同 XAML 命名空间标识符。This is the same XAML namespace identifier used in several predecessor Microsoft technologies that also use XAML as a UI definition markup format. 使用相同的标识符是经过深思熟虑的,在将以前定义的 UI 迁移到使用 C++、C# 或 Visual Basic 的 Windows 运行时应用时很有用。The use of the same identifier is deliberate, and is helpful when you migrate previously defined UI to a Windows Runtime app using C++, C#, or Visual Basic.

第二个声明为 XAML 定义的语言元素映射单独的 XAML 命名空间,并将其映射 (通常) 为 "x:" 前缀: xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"The second declaration maps a separate XAML namespace for the XAML-defined language elements, mapping it (typically) to the "x:" prefix: xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns 值和它所映射到的“x:”前缀对于在多个使用 XAML 的前置任务 Microsoft 技术中使用的定义也是相同的。This xmlns value, and the "x:" prefix it is mapped to, is also identical to the definitions used in several predecessor Microsoft technologies that use XAML.

这些声明之间的关系是,XAML 是一种语言定义,Windows 运行时是一种实现,它使用 XAML 作为语言并定义一个特定的词汇表,它的类型在这里供 XAML 引用。The relationship between these declarations is that XAML is a language definition, and the Windows Runtime is one implementation that uses XAML as a language and defines a specific vocabulary where its types are referenced in XAML.

XAML 语言指定某些语言元素,其中每个元素应可通过适用于 XAML 命名空间的 XAML 处理器实现进行访问。The XAML language specifies certain language elements, and each of these should be accessible through XAML processor implementations working against the XAML namespace. 项目模板、示例代码和语言特性文档遵循 XAML 语言 XAML 命名空间的“x:”映射约定。The "x:" mapping convention for the XAML language XAML namespace is followed by project templates, sample code, and the documentation for language features. XAML 语言命名空间定义多个常用的功能,甚至对于使用 C++、C# 或 Visual Basic 的基本 Windows 运行时应用,这些功能也是必要的。The XAML language namespace defines several commonly used features that are necessary even for basic Windows Runtime apps using C++, C#, or Visual Basic. 例如,为了将任何代码隐藏通过分部类联接到 XAML 文件,必须将该类命名为相关 XAML 文件的根元素中的 x:Class 属性For example, to join any code-behind to a XAML file through a partial class, you must name that class as the x:Class attribute in the root element of the relevant XAML file. 或者,任何在 XAML 页面中定义为 ResourceDictionary 和 XAML 资源引用中一个键资源的元素必须在相关的对象元素上设置 x:Key 特性Or, any element as defined in a XAML page as a keyed resource in a ResourceDictionary and XAML resource references must have the x:Key attribute set on the object element in question.

映射到默认 XAML 命名空间的代码命名空间Code namespaces that map to the default XAML namespace

以下是当前映射到默认 XAML 命名空间的代码命名空间的列表。The folowing is a list of code namespaces that are currently mapped to the default XAML namespace.

  • Windows.UIWindows.UI
  • Windows.UI.XamlWindows.UI.Xaml
  • Windows.UI.Xaml.AutomationWindows.UI.Xaml.Automation
  • Windows.UI.Xaml.Automation.PeersWindows.UI.Xaml.Automation.Peers
  • Windows.UI.Xaml.Automation.ProviderWindows.UI.Xaml.Automation.Provider
  • Windows.UI.Xaml.Automation.TextWindows.UI.Xaml.Automation.Text
  • Windows.UI.Xaml.ControlsWindows.UI.Xaml.Controls
  • Windows.UI.Xaml.Controls.PrimitivesWindows.UI.Xaml.Controls.Primitives
  • Windows.UI.Xaml.DataWindows.UI.Xaml.Data
  • Windows.UI.Xaml.DocumentsWindows.UI.Xaml.Documents
  • Windows.UI.Xaml.InputWindows.UI.Xaml.Input
  • Windows.UI.Xaml.InteropWindows.UI.Xaml.Interop
  • Windows.UI.Xaml.MarkupWindows.UI.Xaml.Markup
  • Windows.UI.Xaml.MediaWindows.UI.Xaml.Media
  • Windows.UI.Xaml.Media.AnimationWindows.UI.Xaml.Media.Animation
  • Windows.UI.Xaml.Media.ImagingWindows.UI.Xaml.Media.Imaging
  • Windows.UI.Xaml.Media.Media3DWindows.UI.Xaml.Media.Media3D
  • Windows.UI.Xaml.NavigationWindows.UI.Xaml.Navigation
  • Windows.UI.Xaml.ResourcesWindows.UI.Xaml.Resources
  • Windows.UI.Xaml.ShapesWindows.UI.Xaml.Shapes
  • Windows UI。Windows.UI.Xaml.Threading
  • Windows.UI.TextWindows.UI.Text

其他 XAML 命名空间Other XAML namespaces

除了默认命名空间和 XAML 语言 XAML 命名空间“x:”,你也可能在 Microsoft Visual Studio 生成的应用的初始默认 XAML 中看到其他的已映射 XAML 命名空间。In addition to the default namespace and the XAML language XAML namespace "x:", you may also see other mapped XAML namespaces in the initial default XAML for apps as generated by Microsoft Visual Studio.

**d: (http://schemas.microsoft.com/expression/blend/2008) **d: (http://schemas.microsoft.com/expression/blend/2008)

“d:”XAML 命名空间旨在提供设计器支持,尤其是 Microsoft Visual Studio 的 XAML 设计界面中的设计器支持。The "d:" XAML namespace is intended for designer support, specifically designer support in the XAML design surfaces of Microsoft Visual Studio. “d:”XAML 命名空间支持 XAML 元素上的设计器或设计时特性。The" d:" XAML namespace enables designer or design-time attributes on XAML elements. 这些设计器特性只影响 XAML 行为的设计方面。These designer attributes affect only the design aspects of how XAML behaves. 如果 Windows 运行时 XAML 分析器在一个应用运行时加载相同的 XAML,设计器特性会被忽略。The designer attributes are ignored when the same XAML is loaded by the Windows Runtime XAML parser when an app runs. 一般而言,设计器特性在任何 XAML 元素上是有效的,但在实际情况中,只有某些场景适合应用设计器特性。Generally, the designer attributes are valid on any XAML element, but in practice there are only certain scenarios where applying a designer attribute yourself is appropriate. 具体来讲,许多设计器特性是为了在你开发使用数据绑定的 XAML 和代码时,提供一种与数据上下文和数据源交互的更好体验。In particular, many of the designer attributes are intended to provide a better experience for interacting with data contexts and data sources while you are developing XAML and code that use data binding.

  • d:DesignHeight 和 d:DesignWidth 属性: 这些属性有时应用于 Visual Studio 或其他 XAML 设计器图面为你创建的 XAML 文件的根。d:DesignHeight and d:DesignWidth attributes: These attributes are sometimes applied to the root of a XAML file that Visual Studio or another XAML designer surface creates for you. 例如,如果你向应用项目中添加了新的 UserControl,则这些属性是针对所创建的 XAML 的 UserControl 根设置的。For example, these attributes are set on the UserControl root of the XAML that is created if you add a new UserControl to your app project. 这些属性更便于设计 XAML 内容组合,以便在将该 XAML 内容用于控件示例或更大 UI 页面的其他部分之后,你能够预测可能存在的布局约束。These attributes make it easier to design the composition of the XAML content, so that you have some anticipation of the layout constraints that might exist once that XAML content is used for a control instance or other part of a larger UI page.

注意   如果要从 Microsoft Silverlight 迁移 XAML,则可能会在表示整个 UI 页的根元素上具有这些属性。Note  If you are migrating XAML from Microsoft Silverlight you might have these attributes on root elements that represent an entire UI page. 在这种情况下,你可能希望删除这些特性。You might want to remove the attributes in this case. 与使用 d:DesignHeightd:DesignWidth 的固定大小页面布局相比,XAML 设计器的其他功能(如模拟器)对于设计能够很好地处理缩放和视图状态的页面布局或许更有用。Other features of the XAML designers such as the simulator are probably more useful for designing page layouts that handle scaling and view states well than is a fixed size page layout using d:DesignHeight and d:DesignWidth.

  • d:DataContext 特性: 可以针对页面根或控件设置此特性,以便替代该对象所拥有的任何显式或继承的 DataContextd:DataContext attribute: You can set this attribute on a page root or a control to override any explicit or inherited DataContext that object otherwise has.
  • d:DesignSource 特性:CollectionViewSource 指定设计时数据源,并替代 Sourced:DesignSource attribute: Specifies a design-time data source for a CollectionViewSource, overriding Source.
  • d:DesignInstance 和 d:DesignData 标记扩展: 这些标记扩展用于为 d:DataContextd:DesignSource 提供设计时数据资源。d:DesignInstance and d:DesignData markup extensions: These markup extensions are used to provide the design-time data resources for either d:DataContext or d:DesignSource. 在这里,我们不会完全记录如何使用设计时数据源资源。We won't fully document how to use design-time data resources here. 有关详细信息,请参阅设计时特性For more info, see Design-Time Attributes. 有关一些使用示例,请参阅设计面图以及用于原型制作的示例数据For some usage examples, see Sample data on the design surface, and for prototyping.

**mc: (http://schemas.openxmlformats.org/markup-compatibility/2006) **mc: (http://schemas.openxmlformats.org/markup-compatibility/2006)

“mc:”表示并支持读取 XAML 的标记兼容性模式。" mc:" indicates and supports a markup compatibility mode for reading XAML. 通常,“d:”前缀与特性 mc:Ignorable 相关联。Typically, the "d:" prefix is associated with the attribute mc:Ignorable. 此技术使运行时 XAML 分析器忽略“d:”中的设计特性。This technique enables run-time XAML parsers to ignore the design attributes in "d:".

local:common:local: and common:

“local:”是一个前缀,通常会在模板化 UWP 应用项目的 XAML 页面中为你映射它。"local:" is a prefix that is often mapped for you within the XAML pages for a templated UWP app project. 它映射为引用相同的命名空间,该命名空间旨在包含 x:Class 特性和所有 XAML 文件(包括 app.xaml)的代码。It's mapped to refer to the same namespace that's created to contain the x:Class attribute and code for all the XAML files including app.xaml. 只要你在此相同命名空间中定义你要在 XAML 中使用的任何自定义类,你就可以使用 local: 前缀在 XAML 中引用你的自定义类型。So long as you define any custom classes you want to use in XAML in this same namespace, you can use the local: prefix to refer to your custom types in XAML. 来自模板化的 UWP 应用项目的相关前缀是 common:A related prefix that comes from a templated UWP app project is common:. 此前缀引用包含实用工具类(如转换器和命令)的嵌套 "Common" 命名空间,您可以在 " 解决方案资源管理器 " 视图中的 "common" 文件夹中找到这些定义。This prefix refers to a nested "Common" namespace that contains utility classes such as converters and commands, and you can find the definitions in the Common folder in the Solution Explorer view.

vsm:vsm:

请勿使用。Do not use. “vsm:”是有时在从其他 Microsoft 技术导入的较老 XAML 模板中会看到的一个前缀。"vsm:" is a prefix that is sometimes seen in older XAML templates imported from other Microsoft technologies. 该命名空间最初解决了旧版命名空间工具问题。The namespace originally addressed a legacy namespace tooling issue. 你应该在用于 Windows 运行时的任何 XAML 中删除“vsm:”的 XAML 命名空间定义,更改 VisualStateVisualStateGroup 和相关对象的任何前缀的用法,从而使用默认的 XAML 命名空间。You should delete XAML namespace definitions for "vsm:" in any XAML you use for the Windows Runtime, and change any prefix usages for VisualState, VisualStateGroup and related objects to use the default XAML namespace instead. 有关 XAML 迁移的详细信息,请参阅将 Silverlight 或 WPF XAML/代码迁移到 Windows 运行时应用For more info on XAML migration, see Migrating Silverlight or WPF XAML/code to a Windows Runtime app.

将自定义类型映射到 XAML 命名空间和前缀Mapping custom types to XAML namespaces and prefixes

你可以映射一个 XAML 命名空间,这样可使用 XAML 访问你自己的自定义类型。You can map a XAML namespace so that you can use XAML to access your own custom types. 换句话说,你正在映射一个代码命名空间,因为它存在于一个定义了自定义类型的代码表示中,为它分配一个 XAML 命名空间以及一个前缀供其使用。In other words, you are mapping a code namespace as it exists in a code representation that defines the custom type, and assigning it a XAML namespace along with a prefix for usage. 针对 XAML 的自定义类型可在 Microsoft .NET 语言(C# 或 Microsoft Visual Basic)或 C++ 中定义。Custom types for XAML can be defined either in a Microsoft .NET language (C# or Microsoft Visual Basic) or in C++. 映射通过定义一个 xmlns 前缀来执行。The mapping is made by defining an xmlns prefix. 例如,xmlns:myTypes 定义一个新 XAML 命名空间,通过在所有用法中添加令牌 myTypes: 作为前缀来访问这个命名空间。For example, xmlns:myTypes defines a new XAML namespace that is accessed by prefixing all usages with the token myTypes:.

xmlns 定义包含一个值以及前缀命名。An xmlns definition includes a value as well as the prefix naming. 该值是一个包含在引号内的字符串,后跟一个等号。The value is a string that goes inside quotation marks, following an equal sign. 一种常见的 XML 约定是将 XML 命名空间与一个统一资源标识符 (URI) 相关联,这样就实现了唯一性和标识约定。A common XML convention is to associate the XML namespace with a Uniform Resource Identifier (URI), so that there is a convention for uniqueness and identification. 你也可以在默认 XAML 命名空间和 XAML 语言 XAML 命名空间中看到此约定,也可以在 Windows 运行时 XAML 所使用的且不太常见的 XAML 命名空间中看到此约定。You also see this convention for the default XAML namespace and the XAML language XAML namespace, as well as for some lesser-used XAML namespaces that are used by Windows Runtime XAML. 对于映射自定义类型(而不是指定一个 URI)的 XAML 命名空间,你可以为定义添加令牌“using:”作为前缀。But for a XAML namespace that maps custom types, instead of specifying a URI, you begin the prefix definition with the token "using:". 在“using:”令牌后,可命名代码命名空间。Following the "using:" token, you then name the code namespace.

例如,若要映射用于引用 "CustomClasses" 命名空间的 "custom1" 前缀,并使用该命名空间或程序集中的类作为 XAML 中的对象元素,XAML 页应在根元素上包括以下映射: xmlns:custom1="using:CustomClasses"For example, to map a "custom1" prefix that enables you to reference a "CustomClasses" namespace, and use classes from that namespace or assembly as object elements in XAML, your XAML page should include the following mapping on the root element: xmlns:custom1="using:CustomClasses"

不需要映射同一页面范围中的分部类。Partial classes of the same page scope do not need to be mapped. 例如,不需要前缀即可引用你为处理来自页面的 XAML UI 定义的事件而定义的任何事件处理程序。For example, you don't need prefixes to reference any event handlers that you defined for handling events from the XAML UI definition of your page. 另外,Visual Studio 生成的使用 C++、C# 或 Visual Basic 的 Windows 运行时应用项目的许多起始 XAML 页面已映射“local:”前缀,它引用项目指定的默认命名空间和分部类定义所使用的命名空间。Also, many of the starting XAML pages from Visual Studio generated projects for a Windows Runtime app using C++, C#, or Visual Basic already map a "local:" prefix, which references the project-specified default namespace and the namespace used by partial class definitions.

CLR 语言规则CLR language rules

如果使用 .NET 语言(C# 或 Microsoft Visual Basic)编写支持代码,你可能会在命名空间名称中使用一个点(“.”)的约定,以创建代码命名空间的概念性层次结构。If you are writing your backing code in a .NET language (C# or Microsoft Visual Basic), you might be using conventions that use a dot (".") as part of namespace names to create a conceptual hierarchy of code namespaces. 如果命名空间定义包含一个点,则这个点应该是你在“using:”令牌之后指定的值的一部分。If your namespace definition contains a dot, the dot should be part of the value you specify after the "using:" token.

如果代码隐藏文件或代码定义文件是 C++ 文件,那么某些约定仍然遵守公共语言运行时 (CLR) 语言形式,因此在 XAML 语法上没有区别。If your code-behind file or code definition file is a C++ file, there are certain conventions that still follow the common language runtime (CLR) language form, so that there is no difference in the XAML syntax. 如果在 C++ 中声明嵌套的命名空间,则在指定“using:”令牌后的值时,连续的嵌套命名空间字符串之间的分隔符也应是一个“.”,而不是“::”。If you declare nested namespaces in C++, the separator between the successive nested namespace strings should be "." rather than "::" when you specify the value that follows the "using:" token.

当你定义代码以供使用 XAML 时,请勿使用嵌套类型(例如在某个类中嵌套枚举)。Don't use nested types (such as nesting an enumeration within a class) when you define your code for use with XAML. 无法评估嵌套类型。Nested types can't be evaluated. XAML 分析器无法区分某个点是嵌套类型名称的一部分,而不是命名空间名称的一部分。There's no way for the XAML parser to distinguish that a dot is part of the nested type name rather than part of the namespace name.

自定义类型和程序集Custom types and assemblies

定义 XAML 命名空间的支持类型的程序集名称不是在映射中指定的。The name of the assembly that defines the backing types for a XAML namespace is not specified in the mapping. 关于哪些程序集可用的逻辑在应用定义级别控制,包含在基本应用部署和安全原则中。The logic for which assemblies are available is controlled at the app-definition level and is part of basic app deployment and security principles. 在项目设置中,将你希望作为 XAML 的一个代码定义源包含的任何程序集声明为一个独立程序集。Declare any assembly that you want included as a code-definition source for XAML as a dependent assembly in project settings. 有关详细信息,请参阅在 C# 和 Visual Basic 中创建 Windows 运行时组件For more info, see Creating Windows Runtime components in C# and Visual Basic.

如果从主要应用的应用程序定义或页面定义中引用自定义类型,这些类型无需进一步的依赖程序集配置即可使用,但你仍然必须映射包含这些类型的代码命名空间。If you are referencing custom types from the primary app's application definition or page definitions, those types are available without further dependent assembly configuration, but you still must map the code namespace that contains those types. 一种常见的约定是映射任何给定 XAML 页面的默认代码命名空间的前缀“local”。A common convention is to map the prefix "local" for the default code namespace of any given XAML page. 此约定常常包含在 XAML 项目的初始项目模板中。This convention is often included in starting project templates for XAML projects.

附加属性Attached properties

如果你引用附加属性,附加属性名称的所有者键入部分必须在默认 XAML 命名空间中,或者必须带有前缀。If you are referencing attached properties, the owner-type portion of the attached property name must either be in the default XAML namespace or be prefixed. 很少会独立于属性元素向属性添加前缀,但这种情况有时是必需的,特别是对于自定义附加属性而言。It's rare to prefix attributes separately from their elements but this is one case where it's sometimes required, particularly for a custom attached property. 有关详细信息,请参阅自定义附加属性For more info, see Custom attached properties.