WPF 中的 XAML 概述XAML overview in WPF

本本介绍 XAML 语言的功能,并演示如何使用 XAML 编写 Windows Presentation Foundation (WPF) 应用。This article describes the features of the XAML language and demonstrates how you can use XAML to write Windows Presentation Foundation (WPF) apps. 本主题专门介绍 WPF 实现的 XAML。This article specifically describes XAML as implemented by WPF. XAML 本身是一个比 WPF 更大的语言概念。XAML itself is a larger language concept than WPF.

重要

桌面指南文档正在撰写中。The Desktop Guide documentation is under construction.

什么是 XAMLWhat is XAML

XAML 是一种声明性标记语言。XAML is a declarative markup language. 应用于 .NET Core 编程模型时,XAML 简化了为 .NET Core 应用创建 UI 的过程。As applied to the .NET Core programming model, XAML simplifies creating a UI for a .NET Core app. 你可以在声明性 XAML 标记中创建可见的 UI 元素,然后使用代码隐藏文件(这些文件通过分部类定义与标记相联接)将 UI 定义与运行时逻辑相分离。You can create visible UI elements in the declarative XAML markup, and then separate the UI definition from the run-time logic by using code-behind files that are joined to the markup through partial class definitions. XAML 直接以程序集中定义的一组特定后备类型表示对象的实例化。XAML directly represents the instantiation of objects in a specific set of backing types defined in assemblies. 这与大多数其他标记语言不同,后者通常是与后备类型系统没有此类直接关系的解释语言。This is unlike most other markup languages, which are typically an interpreted language without such a direct tie to a backing type system. XAML 实现了一个工作流,通过此工作流,各方可以采用不同的工具来处理 UI 和应用的逻辑。XAML enables a workflow where separate parties can work on the UI and the logic of an app, using potentially different tools.

以文本表示时,XAML 文件是通常具有 .xaml 扩展名的 XML 文件。When represented as text, XAML files are XML files that generally have the .xaml extension. 可通过任何 XML 编码对文件进行编码,但通常以 UTF-8 编码。The files can be encoded by any XML encoding, but encoding as UTF-8 is typical.

以下示例演示如何创建 UI 中的按钮。The following example shows how you might create a button as part of a UI. 此示例用于初步了解 XAML 如何表示常用 UI 编程形式(它不是一个完整的示例)。This example is intended to give you a flavor of how XAML represents common UI programming metaphors (it is not a complete sample).

<StackPanel>
  <Button Content="Click Me"/>
</StackPanel>

XAML 语法概述XAML syntax in brief

下面章节介绍 XAML 语法的基本形式,并提供一个简短的标记示例。The following sections explain the basic forms of XAML syntax, and give a short markup example. 这些章节并不提供每个语法形式的完整信息,例如这些语法形式如何在后备类型系统中表示。These sections are not intended to provide complete information about each syntax form, such as how these are represented in the backing type system. 有关 XAML 语法细节的详细信息,请参阅 XAML 语法详述For more information about the specifics of XAML syntax, see XAML Syntax In Detail.

如果已熟悉 XML 语言,则下面几节中的很多材料对你而言都是基础知识。Much of the material in the next few sections will be elementary to you if you have previous familiarity with the XML language. 这得益于 XAML 的一个基本设计原则。This is a consequence of one of the basic design principles of XAML. XAML 语言定义它自己的概念,但这些概念也适用于 XML 语言和标记形式。The XAML language defines concepts of its own, but these concepts work within the XML language and markup form.

XAML 对象元素XAML object elements

对象元素通常声明类型的实例。An object element typically declares an instance of a type. 该类型在将 XAML 用作语言的技术所引用的程序集中定义。That type is defined in the assemblies referenced by the technology that uses XAML as a language.

对象元素语法始终以左尖括号 (<) 开头。Object element syntax always starts with an opening angle bracket (<). 后跟要创建实例的类型的名称。This is followed by the name of the type where you want to create an instance. (该名称可能包含前缀,下文将解释前缀的概念。)此后可以选择声明该对象元素的特性。(The name can include a prefix, a concept that will be explained later.) After this, you can optionally declare attributes on the object element. 要完成对象元素标记,请以右尖括号 (>) 结尾。To complete the object element tag, end with a closing angle bracket (>). 也可以使用不含任何内容的自结束形式,方法是用一个正斜杠后接一个右尖括号 (/>) 来完成标记。You can instead use a self-closing form that does not have any content, by completing the tag with a forward slash and closing angle bracket in succession (/>). 例如,请再次查看之前演示的标记片段。For example, look at the previously shown markup snippet again.

<StackPanel>
  <Button Content="Click Me"/>
</StackPanel>

这指定了两个对象元素:<StackPanel>(含有内容,后面有一个结束标记)和 <Button .../>(自结束形式,包含几个特性)。This specifies two object elements: <StackPanel> (with content, and a closing tag later), and <Button .../> (the self-closing form, with several attributes). 对象元素 StackPanelButton 各映射到一个类名,该类由 WPF 定义并且属于 WPF 程序集。The object elements StackPanel and Button each map to the name of a class that is defined by WPF and is part of the WPF assemblies. 指定对象元素标记时,会创建一条指令,指示 XAML 处理创建基础类型的新实例。When you specify an object element tag, you create an instruction for XAML processing to create a new instance of the underlying type. 每个实例都是在分析和加载 XAML 时通过调用基础类型的无参数构造函数来创建。Each instance is created by calling the parameterless constructor of the underlying type when parsing and loading the XAML.

特性语法(属性)Attribute syntax (properties)

对象的属性通常可表示为对象元素的特性。Properties of an object can often be expressed as attributes of the object element. 特性语法对设置的对象属性命名,后跟赋值运算符 (=)。The attribute syntax names the object property that is being set, followed by the assignment operator (=). 特性的值始终指定为包含在引号中的字符串。The value of an attribute is always specified as a string that is contained within quotation marks.

特性语法是最简化的属性设置语法,并且对曾使用过标记语言的开发人员而言是最直观的语法。Attribute syntax is the most streamlined property setting syntax and is the most intuitive syntax to use for developers who have used markup languages in the past. 例如,以下标记将创建一个具有红色文本和蓝色背景的按钮,还将创建指定为 Content 的显示文本。For example, the following markup creates a button that has red text and a blue background in addition to display text specified as Content.

<Button Background="Blue" Foreground="Red" Content="This is a button"/>

属性元素语法Property element syntax

对于对象元素的某些属性,无法使用特性语法,因为无法在特性语法的引号和字符串限制内充分地表达提供属性值所必需的对象或信息。For some properties of an object element, attribute syntax is not possible, because the object or information necessary to provide the property value cannot be adequately expressed within the quotation mark and string restrictions of attribute syntax. 对于这些情况,可以使用另一个语法,即属性元素语法。For these cases, a different syntax known as property element syntax can be used.

属性元素开始标记的语法为 <TypeName.PropertyName>The syntax for the property element start tag is <TypeName.PropertyName>. 通常,该标记的内容是类型的对象元素,属性会将该元素作为其值。Generally, the content of that tag is an object element of the type that the property takes as its value. 指定内容之后,必须用结束标记结束属性元素。After specifying the content, you must close the property element with an end tag. 结束标记的语法为 </TypeName.PropertyName>The syntax for the end tag is </TypeName.PropertyName>.

如果可以使用特性语法,那么使用特性语法通常更为方便,且能够实现更为精简的标记,但这通常只是样式问题,而不是技术限制。If an attribute syntax is possible, using the attribute syntax is typically more convenient and enables a more compact markup, but that is often just a matter of style, not a technical limitation. 以下示例演示在前面的特性语法示例中设置的相同属性,但这次对 Button 的所有属性使用属性元素语法。The following example shows the same properties being set as in the previous attribute syntax example, but this time by using property element syntax for all properties of the Button.

<Button>
  <Button.Background>
    <SolidColorBrush Color="Blue"/>
  </Button.Background>
  <Button.Foreground>
    <SolidColorBrush Color="Red"/>
  </Button.Foreground>
  <Button.Content>
    This is a button
  </Button.Content>
</Button>

集合语法Collection syntax

XAML 语言包含一些优化,可以生成更易于阅读的标记。The XAML language includes some optimizations that produce more human-readable markup. 其中一项优化是:如果某个特定属性采用集合类型,则在标记中声明为该属性的值内的子元素的项将成为集合的一部分。One such optimization is that if a particular property takes a collection type, then items that you declare in markup as child elements within that property's value become part of the collection. 在这种情况下,子对象元素的集合是设置为集合属性的值。In this case a collection of child object elements is the value being set to the collection property.

下面的示例演示用于设置 GradientStops 属性的值的集合语法。The following example shows collection syntax for setting values of the GradientStops property.

<LinearGradientBrush>
  <LinearGradientBrush.GradientStops>
    <!-- no explicit new GradientStopCollection, parser knows how to find or create -->
    <GradientStop Offset="0.0" Color="Red" />
    <GradientStop Offset="1.0" Color="Blue" />
  </LinearGradientBrush.GradientStops>
</LinearGradientBrush>

XAML 内容属性XAML content properties

XAML 指定了一个语言功能,通过该功能,类可以指定它的一个且仅一个属性为 XAML 内容 属性。XAML specifies a language feature whereby a class can designate exactly one of its properties to be the XAML content property. 该对象元素的子元素用于设置该内容属性的值。Child elements of that object element are used to set the value of that content property. 换言之,仅对内容属性而言,可以在 XAML 标记中设置该属性时省略属性元素,并在标记中生成更直观的父级/子级形式。In other words, for the content property uniquely, you can omit a property element when setting that property in XAML markup and produce a more visible parent/child metaphor in the markup.

例如,Border 指定 Child 的内容 属性。For example, Border specifies a content property of Child. 以下两个 Border 元素的处理方式相同。The following two Border elements are treated identically. 第一个元素利用内容属性语法并省略 Border.Child 属性元素。The first one takes advantage of the content property syntax and omits the Border.Child property element. 第二个元素显式显示 Border.ChildThe second one shows Border.Child explicitly.

<Border>
  <TextBox Width="300"/>
</Border>
<!--explicit equivalent-->
<Border>
  <Border.Child>
    <TextBox Width="300"/>
  </Border.Child>
</Border>

作为 XAML 语言的规则,XAML 内容属性的值必须完全在该对象元素的其他任何属性元素之前或之后指定。As a rule of the XAML language, the value of a XAML content property must be given either entirely before or entirely after any other property elements on that object element. 例如,以下标记不会进行编译。For instance, the following markup does not compile.

<Button>I am a
  <Button.Background>Blue</Button.Background>
  blue button</Button>

有关 XAML 语法细节的详细信息,请参阅 XAML 语法详述For more information about the specifics of XAML syntax, see XAML Syntax In Detail.

文本内容Text content

有少量 XAML 元素可直接将文本作为其内容来处理。A small number of XAML elements can directly process text as their content. 若要实现此功能,必须满足以下条件之一:To enable this, one of the following cases must be true:

  • 类必须声明一个内容属性,并且该内容属性必须是可赋值给字符串的类型(该类型可以是 Object)。The class must declare a content property, and that content property must be of a type assignable to a string (the type could be Object). 例如,任何 ContentControl 都使用 Content 作为其内容属性,并且它属于类型 Object,这对于 ContentControl(如 Button)支持以下用法:<Button>Hello</Button>For instance, any ContentControl uses Content as its content property and it is type Object, and this supports the following usage on a ContentControl such as a Button: <Button>Hello</Button>.

  • 类型必须声明一个类型转换器,该类型转换器将文本内容用作初始化文本。The type must declare a type converter, in which case the text content is used as initialization text for that type converter. 例如,<Brush>Blue</Brush>Blue 的内容值转换为画笔。For example, <Brush>Blue</Brush> converts the content value of Blue into a brush. 这种情况实际上并不常见。This case is less common in practice.

  • 类型必须为已知的 XAML 语言基元。The type must be a known XAML language primitive.

内容属性和集合语法组合Content properties and collection syntax combined

考虑以下示例。Consider this example.

<StackPanel>
  <Button>First Button</Button>
  <Button>Second Button</Button>
</StackPanel>

此处,每个 Button 都是 StackPanel 的子元素。Here, each Button is a child element of StackPanel. 这是一个简单直观的标记,此标记由于两个不同的原因省略了两个标记。This is a streamlined and intuitive markup that omits two tags for two different reasons.

<StackPanel>
  <StackPanel.Children>
    <!--<UIElementCollection>-->
    <Button>First Button</Button>
    <Button>Second Button</Button>
    <!--</UIElementCollection>-->
  </StackPanel.Children>
</StackPanel>

特性语法(事件)Attribute syntax (events)

特性语法还可用于事件成员,而非属性成员。Attribute syntax can also be used for members that are events rather than properties. 在这种情况下,特性的名称为事件的名称。In this case, the attribute's name is the name of the event. 在 XAML 事件的 WPF 实现中,特性的值是实现该事件的委托的处理程序的名称。In the WPF implementation of events for XAML, the attribute's value is the name of a handler that implements that event's delegate. 例如,以下标记将 Click 事件的处理程序分配给在标记中创建的 ButtonFor example, the following markup assigns a handler for the Click event to a Button created in markup:

<Page 
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="ExampleNamespace.ExamplePage">
  <Button Click="Button_Click" >Click Me!</Button>
</Page>

除此特性语法示例外,还有更多关于 WPF 中的事件和 XAML 的内容。There is more to events and XAML in WPF than just this example of the attribute syntax. 例如,可了解此处引用的 ClickHandler 表示什么,以及它是如何定义的。For example, you might wonder what the ClickHandler referenced here represents and how it is defined. 这将在本文后面的事件和 XAML 代码隐藏一节中介绍。This will be explained in the upcoming Events and XAML code-behind section of this article.

XAML 中的大小写和空白Case and white space in XAML

一般而言,XAML 区分大小写。In general, XAML is case-sensitive. 出于解析后备类型的目的,WPF XAML 按照 CLR 区分大小写的相同规则区分大小写。For purposes of resolving backing types, WPF XAML is case-sensitive by the same rules that the CLR is case-sensitive. 以下情况下,对象元素、属性元素和特性名称均必须使用区分大小写的形式指定:按名称与程序集中的基础类型进行比较或者与类型的成员进行比较。Object elements, property elements, and attribute names must all be specified by using the sensitive casing when compared by name to the underlying type in the assembly, or to a member of a type. XAML 语言关键字和基元也区分大小写。XAML language keywords and primitives are also case-sensitive. 值并不总是区分大小写。Values are not always case-sensitive. 值是否区分大小写将取决于与采用该值的属性关联的类型转换器行为,或取决于属性值类型。Case sensitivity for values will depend on the type converter behavior associated with the property that takes the value, or the property value type. 例如,采用 Boolean 类型的属性可以采用 trueTrue 作为等效值,但只是因为将字符串转换为 Boolean 的本机 WPF XAML 分析程序类型转换已经允许将这些值作为等效值。For example, properties that take the Boolean type can take either true or True as equivalent values, but only because the native WPF XAML parser type conversion for string to Boolean already permits these as equivalents.

WPF XAML 处理器和序列化程序将忽略或删除所有无意义的空白,并标准化任何有意义的空白。WPF XAML processors and serializers will ignore or drop all nonsignificant white space, and will normalize any significant white space. 这与 XAML 规范的默认空白行为建议一致。This is consistent with the default white-space behavior recommendations of the XAML specification. 只有在 XAML 内容属性中指定字符串时,此行为的重要性才会体现出来。This behavior is only of consequence when you specify strings within XAML content properties. 简言之,XAML 将空格、换行符和制表符转化为空格,如果它们出现在一个连续字符串的任一端,则保留一个空格。In simplest terms, XAML converts space, linefeed, and tab characters into spaces, and then preserves one space if found at either end of a contiguous string. 本文不包含有关 XAML 空白处理的完整说明。The full explanation of XAML white-space handling is not covered in this article. 有关详细信息,请参阅 XAML 中的空白处理For more information, see White space processing in XAML.

标记扩展Markup extensions

标记扩展是一个 XAML 语言概念。Markup extensions are a XAML language concept. 用于提供特性语法的值时,大括号({})表示标记扩展用法。When used to provide the value of an attribute syntax, curly braces ({ and }) indicate a markup extension usage. 此用法指示 XAML 处理不要像通常那样将特性值视为文本字符串或者可转换为字符串的值。This usage directs the XAML processing to escape from the general treatment of attribute values as either a literal string or a string-convertible value.

WPF 应用编程中最常用的标记扩展是 Binding(用于数据绑定表达式)以及资源引用 StaticResourceDynamicResourceThe most common markup extensions used in WPF app programming are Binding, used for data binding expressions, and the resource references StaticResource and DynamicResource. 通过使用标记扩展,即使属性通常不支持特性语法,也可以使用特性语法为属性提供值。By using markup extensions, you can use attribute syntax to provide values for properties even if that property does not support an attribute syntax in general. 标记扩展经常使用中间表达式类型实现一些功能,例如,推迟值或引用仅在运行时才存在的其他对象。Markup extensions often use intermediate expression types to enable features such as deferring values or referencing other objects that are only present at run-time.

例如,以下标记使用特性语法设置 Style 属性的值。For example, the following markup sets the value of the Style property using attribute syntax. Style 属性采用 Style 类的实例,该类在默认情况下无法由特性语法字符串进行实例化。The Style property takes an instance of the Style class, which by default could not be instantiated by an attribute syntax string. 但在本例中,特性引用了特定的标记扩展 StaticResourceBut in this case, the attribute references a particular markup extension, StaticResource. 处理该标记扩展时,将返回对以前在资源字典中作为键控资源进行实例化的某个样式的引用。When that markup extension is processed, it returns a reference to a style that was previously instantiated as a keyed resource in a resource dictionary.

<Page.Resources>
  <SolidColorBrush x:Key="MyBrush" Color="Gold"/>
  <Style TargetType="Border" x:Key="PageBackground">
    <Setter Property="Background" Value="Blue"/>
  </Style>
</Page.Resources>
<StackPanel>
  <Border Style="{StaticResource PageBackground}">
  </Border>
</StackPanel>

有关特定在 WPF 中实现的所有 XAML 标记扩展的参考列表,请参阅 WPF XAML 扩展For a reference listing of all markup extensions for XAML implemented specifically in WPF, see WPF XAML Extensions. 有关由 System.Xaml 定义并且可更广泛用于 .NET Core XAML 实现的标记扩展的参考列表,请参阅 XAML 命名空间 (x:)语言功能For a reference listing of the markup extensions that are defined by System.Xaml and are more widely available for .NET Core XAML implementations, see XAML Namespace (x:) Language Features. 有关标记扩展概念的详细信息,请参阅标记扩展和 WPF XAMLFor more information about markup extension concepts, see Markup Extensions and WPF XAML.

类型转换器Type converters

XAML 语法概述一节中,曾提到特性值必须能够通过字符串进行设置。In the XAML Syntax in Brief section, it was stated that the attribute value must be able to be set by a string. 对字符串如何转换为其他对象类型或基元值的基本本机处理取决于 String 类型本身,以及对某些类型(如 DateTimeUri)的本机处理。The basic, native handling of how strings are converted into other object types or primitive values is based on the String type itself, in addition to native processing for certain types such as DateTime or Uri. 但是很多 WPF 类型或这些类型的成员扩展了基本字符串特性处理行为,因此可以将更复杂的对象类型的实例指定为字符串和特性。But many WPF types or members of those types extend the basic string attribute processing behavior in such a way that instances of more complex object types can be specified as strings and attributes.

Thickness 结构是一个类型示例,该类型拥有可使用 XAML 的类型转换。The Thickness structure is an example of a type that has a type conversion enabled for XAML usages. Thickness 指示嵌套矩形中的度量,可用作属性(如 Margin)的值。Thickness indicates measurements within a nested rectangle and is used as the value for properties such as Margin. 通过对 Thickness 放置类型转换器,所有使用 Thickness 的属性都可以更容易地在 XAML 中指定,因为它们可指定为特性。By placing a type converter on Thickness, all properties that use a Thickness are easier to specify in XAML because they can be specified as attributes. 以下示例使用类型转换和特性语法来为 Margin 提供值:The following example uses a type conversion and attribute syntax to provide a value for a Margin:

<Button Margin="10,20,10,30" Content="Click me"/>

以上特性语法示例与下面更为详细的语法示例等效,但在下面的示例中,Margin 改为通过包含 Thickness 对象元素的属性元素语法进行设置。The previous attribute syntax example is equivalent to the following more verbose syntax example, where the Margin is instead set through property element syntax containing a Thickness object element. 而且将 Thickness 的四个关键属性设置为新实例的特性:The four key properties of Thickness are set as attributes on the new instance:

<Button Content="Click me">
  <Button.Margin>
    <Thickness Left="10" Top="20" Right="10" Bottom="30"/>
  </Button.Margin>
</Button>

备注

对于少数对象,类型转换是在不涉及子类的情况下将属性设置为此类型的唯一公开方式,因为类型自身没有无参数构造函数。There are also a limited number of objects where the type conversion is the only public way to set a property to that type without involving a subclass, because the type itself does not have a parameterless constructor. 示例为 CursorAn example is Cursor.

有关类型转换的详细信息,请参阅 TypeConverters 和 XAMLFor more information on type conversion, see TypeConverters and XAML.

XAML 根元素和 XAML 命名空间XAML root elements and XAML namespaces

一个 XAML 文件只能有一个根元素,这样才能同时作为格式正确的 XML 文件和有效的 XAML 文件。A XAML file must have only one root element, in order to be both a well-formed XML file and a valid XAML file. 对于典型 WPF 方案,可使用在 WPF 应用模型中具有突出意义的根元素(例如,页面的 WindowPage、外部字典的 ResourceDictionary 或应用定义的 Application)。For typical WPF scenarios, you use a root element that has a prominent meaning in the WPF app model (for example, Window or Page for a page, ResourceDictionary for an external dictionary, or Application for the app definition). 以下示例演示 WPF 页的典型 XAML 文件的根元素,此根元素为 PageThe following example shows the root element of a typical XAML file for a WPF page, with the root element of Page.

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
</Page>

根元素还包含特性 xmlnsxmlns:xThe root element also contains the attributes xmlns and xmlns:x. 这些特性向 XAML 处理器指示哪些 XAML 命名空间包含标记将其作为元素引用的后备类型的类型定义。These attributes indicate to a XAML processor which XAML namespaces contain the type definitions for backing types that the markup will reference as elements. xmlns 特性明确指示默认的 XAML 命名空间。The xmlns attribute specifically indicates the default XAML namespace. 在默认的 XAML 命名空间中,可以不使用前缀指定标记中的对象元素。Within the default XAML namespace, object elements in the markup can be specified without a prefix. 对于大多数 WPF 应用程序方案以及 SDK 的 WPF 部分中给出的几乎所有示例,默认的 XAML 命名空间均映射到 WPF 命名空间 http://schemas.microsoft.com/winfx/2006/xaml/presentationFor most WPF app scenarios, and for almost all of the examples given in the WPF sections of the SDK, the default XAML namespace is mapped to the WPF namespace http://schemas.microsoft.com/winfx/2006/xaml/presentation. xmlns:x 特性指示另一个 XAML 命名空间,该命名空间映射 XAML 语言命名空间http://schemas.microsoft.com/winfx/2006/xamlThe xmlns:x attribute indicates an additional XAML namespace, which maps the XAML language namespace http://schemas.microsoft.com/winfx/2006/xaml.

使用 xmlns 定义用法范围和名称范围映射的做法符合 XML 1.0 规范。This usage of xmlns to define a scope for usage and mapping of a namescope is consistent with the XML 1.0 specification. XAML 名称范围与 XML 名称范围的不同仅在于:XAML 名称范围还包含有关进行类型解析和分析 XAML 时名称范围的元素如何受类型支持的信息。XAML namescopes are different from XML namescopes only in that a XAML namescope also implies something about how the namescope's elements are backed by types when it comes to type resolution and parsing the XAML.

只有在每个 XAML 文件的根元素上,xmlns 特性才是绝对必需的。The xmlns attributes are only strictly necessary on the root element of each XAML file. xmlns 定义将适用于根元素的所有子代元素(此行为也符合 xmlns 的 XML 1.0 规范)。同时允许根以下的其他元素上具有 xmlns 特性,这些特性将适用于定义元素的任何子代元素。xmlns definitions will apply to all descendant elements of the root element (this behavior is again consistent with the XML 1.0 specification for xmlns.) xmlns attributes are also permitted on other elements underneath the root, and would apply to any descendant elements of the defining element. 但是,频繁定义或重新定义 XAML 命名空间会导致难以阅读 XAML 标记样式。However, frequent definition or redefinition of XAML namespaces can result in a XAML markup style that is difficult to read.

其 XAML 处理器的 WPF 实现包括可识别 WPF 核心程序集的基础结构。The WPF implementation of its XAML processor includes an infrastructure that has awareness of the WPF core assemblies. 已知 WPF 核心程序集包含支持指向默认 XAML 命名空间的 WPF 映射的类型。The WPF core assemblies are known to contain the types that support the WPF mappings to the default XAML namespace. 这是通过项目生成文件中的配置以及 WPF 生成和项目系统实现的。This is enabled through configuration that is part of your project build file and the WPF build and project systems. 因此,为了引用来自 WPF 程序集的 XAML 元素,只需将默认 XAML 命名空间声明为默认 xmlnsTherefore, declaring the default XAML namespace as the default xmlns is all that is necessary in order to reference XAML elements that come from WPF assemblies.

x: 前缀The x: prefix

在之前的根元素示例中,前缀 x: 用于映射 XAML 命名空间 http://schemas.microsoft.com/winfx/2006/xaml,该命名空间是支持 XAML 语言构造的专用 XAML 命名空间。In the previous root element example, the prefix x: was used to map the XAML namespace http://schemas.microsoft.com/winfx/2006/xaml, which is the dedicated XAML namespace that supports XAML language constructs. 在这整个 SDK 的项目模板、示例以及文档中,此 x: 前缀用于映射该 XAML 命名空间。This x: prefix is used for mapping this XAML namespace in the templates for projects, in examples, and in documentation throughout this SDK. XAML 语言的 XAML 命名空间包含多个将在 XAML 中频繁使用的编程构造。The XAML namespace for the XAML language contains several programming constructs that you will use frequently in your XAML. 下面列出了最常用的 x: 前缀编程构造:The following is a listing of the most common x: prefix programming constructs you will use:

  • x:Key:为 ResourceDictionary(或其他框架中的类似字典概念)中的每个资源设置唯一的键。x:Key: Sets a unique key for each resource in a ResourceDictionary (or similar dictionary concepts in other frameworks). 在典型的 WPF 应用标记中的所有 x: 用法中,x:Key 可能占到 90%。x:Key will probably account for 90 percent of the x: usages you will see in a typical WPF app's markup.

  • x:Class:向为 XAML 页提供代码隐藏的类指定 CLR 命名空间和类名。x:Class: Specifies the CLR namespace and class name for the class that provides code-behind for a XAML page. 必须具有这样一个类才能支持每个 WPF 编程模型的代码隐藏,因此即使没有资源,也几乎总是能看到映射的 x:You must have such a class to support code-behind per the WPF programming model, and therefore you almost always see x: mapped, even if there are no resources.

  • x:Name:处理对象元素后,为运行时代码中存在的实例指定运行时对象名称。x:Name: Specifies a run-time object name for the instance that exists in run-time code after an object element is processed. 通常,经常为 x:Name 使用 WPF 定义的等效属性。In general, you will frequently use a WPF-defined equivalent property for x:Name. 此类属性特定映射到 CLR 后备属性,因此更便于进行应用编程,在应用编程中,经常使用运行时代码从初始化的 XAML 中查找命名元素。Such properties map specifically to a CLR backing property and are thus more convenient for app programming, where you frequently use run-time code to find the named elements from initialized XAML. 最常见的此类属性是 FrameworkElement.NameThe most common such property is FrameworkElement.Name. 在特定类型中不支持等效的 WPF 框架级 Name 属性时,仍然可以使用 x:NameYou might still use x:Name when the equivalent WPF framework-level Name property is not supported in a particular type. 某些动画方案中会发生这种情况。This occurs in certain animation scenarios.

  • x:Static:启用一个返回静态值的引用,该静态值不是与 XAML 兼容的属性。x:Static: Enables a reference that returns a static value that is not otherwise a XAML-compatible property.

  • x:Type:根据类型名称构造 Type 引用。x:Type: Constructs a Type reference based on a type name. 用于指定采用 Type(例如 Style.TargetType)的特性,但属性经常具有本机的字符串到 Type 的转换功能,因此使用 x:Type 标记扩展用法是可选的。This is used to specify attributes that take Type, such as Style.TargetType, although frequently the property has native string-to-Type conversion in such a way that the x:Type markup extension usage is optional.

x: 前缀/XAML 命名空间中还有其他一些不太常见的编程构造。There are additional programming constructs in the x: prefix/XAML namespace, which are not as common. 有关详细信息,请参阅 XAML 命名空间 (x:)语言功能For details, see XAML Namespace (x:) Language Features.

XAML 中的自定义前缀和自定义类型Custom prefixes and custom types in XAML

对于自身的自定义程序集或 PresentationCore 、PresentationFramework 和 WindowsBase 的 WPF 核心以外的程序集,可以将该程序集指定为自定义 xmlns 映射的一部分。For your own custom assemblies, or for assemblies outside the WPF core of PresentationCore, PresentationFramework and WindowsBase, you can specify the assembly as part of a custom xmlns mapping. 只要该类型能够正确地实现以支持正在尝试的 XAML 用法,就可以在 XAML 中引用该程序集中的类型。You can then reference types from that assembly in your XAML, so long as that type is correctly implemented to support the XAML usages you are attempting.

下面是一个说明自定义前缀如何在 XAML 标记中工作的基本示例。The following is a basic example of how custom prefixes work in XAML markup. 前缀 custom 在根元素标记中定义,并映射为打包在应用中并随应用一起提供的特定程序集。The prefix custom is defined in the root element tag, and mapped to a specific assembly that is packaged and available with the app. 此程序集包含 NumericUpDown 类型,实现该类型的目的是在支持常规 XAML 用法之外,还可以使用允许在 WPF XAML 内容模型的此特定点执行插入的类继承。This assembly contains a type NumericUpDown, which is implemented to support general XAML usage as well as using a class inheritance that permits its insertion at this particular point in a WPF XAML content model. 通过使用该前缀,此 NumericUpDown 控件的一个实例声明为对象元素,以便 XAML 分析程序可找到包含该类型的 XAML 命名空间,从而找到包含该类型定义的后备程序集的位置。An instance of this NumericUpDown control is declared as an object element, using the prefix so that a XAML parser knows which XAML namespace contains the type, and therefore where the backing assembly is that contains the type definition.

<Page
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:custom="clr-namespace:NumericUpDownCustomControl;assembly=CustomLibrary"
    >
  <StackPanel Name="LayoutRoot">
    <custom:NumericUpDown Name="numericCtrl1" Width="100" Height="60"/>
...
  </StackPanel>
</Page>

有关 XAML 中自定义类型的详细信息,请参阅 XAML 及 WPF 的自定义类For more information about custom types in XAML, see XAML and Custom Classes for WPF.

若要深入了解 XML 命名空间与程序集中代码命名空间之间的关系,请参阅 WPF XAML 的 XAML 命名空间和命名空间映射For more information about how XML namespaces and code namespaces in assemblies are related, see XAML Namespaces and Namespace Mapping for WPF XAML.

事件和 XAML 代码隐藏Events and XAML code-behind

大多数 WPF 应用既包括 XAML 标记,也包括代码隐藏。Most WPF apps consist of both XAML markup and code-behind. 在一个项目中,XAML 编写为 .xaml 文件,而 CLR 语言(如 Microsoft Visual Basic 或 C#)用于编写代码隐藏文件。Within a project, the XAML is written as a .xaml file, and a CLR language such as Microsoft Visual Basic or C# is used to write a code-behind file. 在 WPF 编程和应用程序模型中对 XAML 文件进行标记编译时,XAML 文件的 XAML 代码隐藏文件的位置是通过如下方式来标识的:以 XAML 根元素的 x:Class 特性形式指定一个命名空间和类。When a XAML file is markup compiled as part of the WPF programming and application models, the location of the XAML code-behind file for a XAML file is identified by specifying a namespace and class as the x:Class attribute of the root element of the XAML.

通过目前已介绍的示例,你已了解了几个按钮,但这其中没有一个按钮具有任何与其关联的逻辑行为。In the examples so far, you have seen several buttons, but none of these buttons had any logical behavior associated with them yet. 为对象元素添加行为的主要应用程序级机制是使用元素类的现有事件,并为在运行时引发该事件时调用的该事件编写特定的处理程序。The primary application-level mechanism for adding a behavior for an object element is to use an existing event of the element class, and to write a specific handler for that event that is invoked when that event is raised at run-time. 在标记中指定事件名称以及要使用的处理程序的名称,而在代码隐藏中定义实现处理程序的代码。The event name and the name of the handler to use are specified in the markup, whereas the code that implements your handler is defined in the code-behind.

<Page 
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="ExampleNamespace.ExamplePage">
  <Button Click="Button_Click" >Click Me!</Button>
</Page>
namespace ExampleNamespace
{
  public partial class ExamplePage
  {
    void Button_Click(object sender, RoutedEventArgs e)
    {
      Button b = e.Source as Button;
      b.Foreground = Brushes.Red;
    }
  }
}
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
    Dim b As Button = e.Source
    b.Foreground = Brushes.Red
End Sub

请注意,代码隐藏文件使用 CLR 命名空间 ExampleNamespace 并将 ExamplePage 声明为该命名空间内的一个分部类。Notice that the code-behind file uses the CLR namespace ExampleNamespace and declares ExamplePage as a partial class within that namespace. 这相当于 ExampleNamespace.ExamplePagex:Class 特性值,This parallels the x:Class attribute value of ExampleNamespace.ExamplePage 前者在标记根中提供。that was provided in the markup root. WPF 标记编译器将通过从根元素类型派生一个类,为编译的任何 XAML 文件创建一个分部类。The WPF markup compiler will create a partial class for any compiled XAML file, by deriving a class from the root element type. 在提供定义同一分部类的代码隐藏时,将在与编译的应用相同的命名空间和类中合并生成的代码。When you provide code-behind that also defines the same partial class, the resulting code is combined within the same namespace and class of the compiled app.

若要深入了解 WPF 中代码隐藏编程的要求,请参阅 WPF 中的代码隐藏、事件处理程序和分部类要求For more information about requirements for code-behind programming in WPF, see Code-behind, Event Handler, and Partial Class Requirements in WPF.

如果不需要创建单独的代码隐藏文件,还可以将代码内联到 XAML 文件中。If you do not want to create a separate code-behind file, you can also inline your code in a XAML file. 但是,内联代码是一种通用性较低的方法,具有很多的限制。However, inline code is a less versatile technique that has substantial limitations. 有关详细信息,请参阅 WPF 中的代码隐藏和 XAMLFor more informaiton, see Code-Behind and XAML in WPF.

路由事件Routed events

路由事件是一个特殊的事件功能,该功能是 WPF 的基础。A particular event feature that is fundamental to WPF is a routed event. 路由事件使一个元素可以处理另一个元素引发的事件,前提是这些元素通过树关系连接在一起。Routed events enable an element to handle an event that was raised by a different element, as long as the elements are connected through a tree relationship. 使用 XAML 特性指定事件处理时,可以对任何元素(包括未在类成员表中列出该特定事件的元素)侦听和处理该路由事件。When specifying event handling with a XAML attribute, the routed event can be listened for and handled on any element, including elements that do not list that particular event in the class members table. 这是通过以所属类名限定事件名特性来实现的。This is accomplished by qualifying the event name attribute with the owning class name. 例如,在当前讨论的 StackPanel / Button 示例中,父 StackPanel 可以在 StackPanel 对象元素上指定特性 Button.Click,并将处理程序名用作特性值,从而为子元素按钮的 Click 事件注册一个处理程序。For instance, the parent StackPanel in the ongoing StackPanel / Button example could register a handler for the child element button's Click event by specifying the attribute Button.Click on the StackPanel object element, with your handler name as the attribute value. 有关详细信息,请参阅路由事件概述For more information, see Routed Events Overview.

XAML 命名元素XAML named elements

默认情况下,通过处理 XAML 对象元素在对象图中创建的对象实例没有唯一标识符或对象引用。By default, the object instance that is created in an object graph by processing a XAML object element does not possess a unique identifier or object reference. 相反,如果在代码中调用构造函数,则几乎总是使用构造函数结果为构造的实例设置变量,以便以后在代码中引用该实例。In contrast, if you call a constructor in code, you almost always use the constructor result to set a variable to the constructed instance, so that you can reference the instance later in your code. 为了对通过标记定义创建的对象提供标准化访问,XAML 定义了 x:Name 特性In order to provide standardized access to objects that were created through a markup definition, XAML defines the x:Name attribute. 可以在任何对象元素上设置 x:Name 特性的值。You can set the value of the x:Name attribute on any object element. 在代码隐藏中,所选标识符等效于引用所构造的实例的实例变量。In your code-behind, the identifier you choose is equivalent to an instance variable that refers to the constructed instance. 在所有方面,命名元素以类似于对象实例的方式工作(名称引用实例),并且代码隐藏可以通过引用命名元素来处理应用内的运行时交互。In all respects, named elements function as if they were object instances (the name references that instance), and your code-behind can reference the named elements to handle run-time interactions within the app. 实例和变量之间的这种连接是由 WPF XAML 标记编译器实现的,并且更具体地涉及到功能和模式,例如本文中未详细讨论的 InitializeComponentThis connection between instances and variables is accomplished by the WPF XAML markup compiler, and more specifically involve features and patterns such as InitializeComponent that will not be discussed in detail in this article.

WPF 框架级 XAML 元素继承 Name 属性,该属性等效于 XAML 定义的 x:Name 特性。WPF framework-level XAML elements inherit a Name property, which is equivalent to the XAML defined x:Name attribute. 其他某些类也为 x:Name(通常也定义为 Name 属性)提供属性级等效项。Certain other classes also provide property-level equivalents for x:Name, which is also generally defined as a Name property. 一般而言,如果在所选元素/类型的成员表中找不到 Name 属性,则可以改用 x:NameGenerally speaking, if you cannot find a Name property in the members table for your chosen element/type, use x:Name instead. x:Name 值将通过特定子系统或通过 FindName 等实用工具方法,为可在运行时使用的 XAML 元素提供标识符。The x:Name values will provide an identifier to a XAML element that can be used at run-time, either by specific subsystems or by utility methods such as FindName.

下面的示例对 StackPanel 元素设置 NameThe following example sets Name on a StackPanel element. 然后,该 StackPanelButton 上的一个处理程序通过其实例引用 buttonContainer(由 Name 设置)来引用 StackPanelThen, a handler on a Button within that StackPanel references the StackPanel through its instance reference buttonContainer as set by Name.

<StackPanel Name="buttonContainer">
  <Button Click="RemoveThis">Click to remove this button</Button>
</StackPanel>
void RemoveThis(object sender, RoutedEventArgs e)
{
    FrameworkElement fe = e.Source as FrameworkElement;
    if (buttonContainer.Children.Contains(fe))
    {
        buttonContainer.Children.Remove(fe);
    }
}
 Private Sub RemoveThis(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
     Dim fe As FrameworkElement = e.Source
     If (buttonContainer.Children.Contains(fe)) Then
         buttonContainer.Children.Remove(fe)
     End If
End Sub

和变量一样,实例的 XAML 名称受范围概念约束,因此可以在可预测的某个范围内强制名称唯一。Just like a variable, the XAML name for an instance is governed by a concept of scope, so that names can be enforced to be unique within a certain scope that is predictable. 定义页面的主标记表示一个唯一的 XAML 名称范围,而该 XAML 名称范围的边界是该页面的根元素。The primary markup that defines a page denotes one unique XAML namescope, with the XAML namescope boundary being the root element of that page. 但是,其他标记源(如样式或样式中的模板)可以在运行时与页面交互,这种标记源常常具有自己的 XAML 名称范围,这些名称范围不一定与页面的 XAML 名称范围相关联。However, other markup sources can interact with a page at run-time, such as styles or templates within styles, and such markup sources often have their own XAML namescopes that do not necessarily connect with the XAML namescope of the page. 有关 x:Name 和 XAML 名称范围的详细信息,请参阅 Namex:Name 指令WPF XAML 名称范围For more information on x:Name and XAML namescopes, see Name, x:Name Directive, or WPF XAML Namescopes.

附加属性和附加事件Attached properties and attached events

XAML 指定了一个语言功能,该功能允许对任何元素指定某些属性或事件,而不管要设置属性或事件的元素的类型定义中是否存在该属性或事件。XAML specifies a language feature that enables certain properties or events to be specified on any element, regardless of whether the property or event exists in the type's definitions for the element it is being set on. 该功能的属性版本称为附加属性,事件版本称为附加事件。The properties version of this feature is called an attached property, the events version is called an attached event. 从概念上讲,可以将附加属性和附加事件视为可以在任何 XAML 元素/对象实例上设置的全局成员。Conceptually, you can think of attached properties and attached events as global members that can be set on any XAML element/object instance. 但是,元素/类或更大的基础结构必须支持附加值的后备属性存储。However, that element/class or a larger infrastructure must support a backing property store for the attached values.

通常通过特性语法来使用 XAML 中的附加属性。Attached properties in XAML are typically used through attribute syntax. 在特性语法中,可以采用 ownerType.propertyName 的形式指定附加属性。In attribute syntax, you specify an attached property in the form ownerType.propertyName.

表面上,这与属性元素用法类似,但在这种情况下,所指定的 ownerType 始终是一种与从中要设置附加属性的对象元素不同的类型。Superficially, this resembles a property element usage, but in this case the ownerType you specify is always a different type than the object element where the attached property is being set. ownerType 这种类型提供 XAML 处理器为获取或设置附加属性值所需要的访问器方法。ownerType is the type that provides the accessor methods that are required by a XAML processor in order to get or set the attached property value.

附加属性的最常见方案是使子元素向其父元素报告属性值。The most common scenario for attached properties is to enable child elements to report a property value to their parent element.

下面的示例演示 DockPanel.Dock 附加属性。The following example illustrates the DockPanel.Dock attached property. DockPanel 类为 DockPanel.Dock 定义访问器,因此拥有附加属性。The DockPanel class defines the accessors for DockPanel.Dock and therefore owns the attached property. DockPanel 类还包括一个逻辑,该逻辑迭代其子元素并具体检查每个元素是否具有 DockPanel.Dock 设置值。The DockPanel class also includes logic that iterates its child elements and specifically checks each element for a set value of DockPanel.Dock. 如果找到一个值,将在布局过程中使用该值定位子元素。If a value is found, that value is used during layout to position the child elements. 使用 DockPanel.Dock 附加属性和此定位功能实际上是 DockPanel 类激动人心的一面。Use of the DockPanel.Dock attached property and this positioning capability is in fact the motivating scenario for the DockPanel class.

<DockPanel>
  <Button DockPanel.Dock="Left" Width="100" Height="20">I am on the left</Button>
  <Button DockPanel.Dock="Right" Width="100" Height="20">I am on the right</Button>
</DockPanel>

在 WPF 中,大部分或所有附加属性还作为依赖属性实现。In WPF, most or all the attached properties are also implemented as dependency properties. 有关详细信息,请参阅附加属性概述For more information, see Attached Properties Overview.

附加事件使用类似的 ownerType.eventName 特性语法形式。Attached events use a similar ownerType.eventName form of attribute syntax. 和非附加事件一样,XAML 中附加事件的特性值指定对元素处理事件时调用的处理程序方法的名称。Just like the non-attached events, the attribute value for an attached event in XAML specifies the name of the handler method that is invoked when the event is handled on the element. 在 WPF XAML 中使用附加事件并不常见。Attached event usages in WPF XAML are less common. 有关详细信息,请参阅附加事件概述For more information, see Attached Events Overview.

基类型和 XAMLBase types and XAML

基础 WPF XAML 及其 XAML 命名空间是类型的一个集合,这些类型对应于 CLR 对象以及 XAML 的标记元素。Underlying WPF XAML and its XAML namespace is a collection of types that correspond to CLR objects in addition to markup elements for XAML. 但是,并不是所有的类都能映射到元素。However, not all classes can be mapped to elements. 抽象类(如 ButtonBase)和某些非抽象基类在 CLR 对象模型中用于继承。Abstract classes, such as ButtonBase, and certain non-abstract base classes, are used for inheritance in the CLR objects model. 基类(包括抽象类)对于 XAML 开发仍然很重要,因为每个具体的 XAML 元素都从其层次结构中的某个基类继承成员。Base classes, including abstract ones, are still important to XAML development because each of the concrete XAML elements inherits members from some base class in its hierarchy. 通常,这些成员包括可以设置为元素特性的属性或者可以处理的事件。Often these members include properties that can be set as attributes on the element, or events that can be handled. FrameworkElement 是 WPF 在 WPF 框架级的具体 UI 基类。FrameworkElement is the concrete base UI class of WPF at the WPF framework level. 设计 UI 时,将使用各种形状、面板、装饰器或控件类,它们全部派生自 FrameworkElementWhen designing UI, you will use various shape, panel, decorator, or control classes, which all derive from FrameworkElement. 一个相关的基类 FrameworkContentElement 使用可在 FrameworkElement 中特意镜像 API 的 API,支持适合流布局表示形式的面向文档的元素。A related base class, FrameworkContentElement, supports document-oriented elements that work well for a flow layout presentation, using APIs that deliberately mirror the APIs in FrameworkElement. 元素级的特性和 CLR 对象模型的组合提供一组通用的属性,这些属性可以在大多数具体的 XAML 元素上设置,而不管具体的 XAML 元素及其基础类型。The combination of attributes at the element level and a CLR object model provides you with a set of common properties that are settable on most concrete XAML elements, regardless of the specific XAML element and its underlying type.

XAML 安全性XAML security

XAML 是一种直接表示对象实例化和执行的标记语言。XAML is a markup language that directly represents object instantiation and execution. 因此,在 XAML 中创建的元素能够像等效的生成代码那样与系统资源(如网络访问、文件系统 IO)进行交互。Therefore, elements created in XAML have the same ability to interact with system resources (network access, file system IO, for example) as the equivalent generated code does. 加载到完全受信任的应用中的 XAML 与承载应用具有相同的系统资源访问权限。XAML loaded in to a fully trusted app has the same access to the system resources as the hosting app does.

WPF 中的代码访问安全性 (CAS)Code Access Security (CAS) in WPF

本部分仅适用于 .NET Framework。适用于 .NET Core 的 WPF 不支持 CAS。有关详细信息,请参阅代码访问安全性差异This section only applies to .NET Framework. WPF for .NET Core doesn't support CAS. For more information, see Code Access Security differences.

适用于 .NET Framework 的 WPF 支持代码访问安全性 (CAS)。WPF for .NET Framework supports Code Access Security (CAS). 这意味着在 Internet 区域中运行的 WPF 内容具有缩减的执行权限。This means that WPF content running in the internet zone has reduced execution permissions. “宽松型 XAML”(由 XAML 查看器在加载时解释的非编译 XAML 的页面)和 XBAP 通常在此 Internet 区域中运行,并且使用相同的权限集。"Loose XAML" (pages of noncompiled XAML interpreted at load time by a XAML viewer) and XBAP are usually run in this internet zone and use the same permission set. 但是,加载到完全受信任的应用程序中的 XAML 与承载应用程序具有相同的系统资源访问权限。However, XAML loaded in to a fully trusted application has the same access to the system resources as the hosting application does. 有关详细信息,请参阅 WPF 部分信任安全性For more information, see WPF Partial Trust Security.

从代码中加载 XAMLLoading XAML from code

XAML 可用于定义整个 UI,但有时也适合在 XAML 中定义一部分 UI。XAML can be used to define all of the UI, but it is sometimes also appropriate to define just a piece of the UI in XAML. 此功能可用于实现部分自定义、在本地存储信息,使用 XAML 提供业务对象或者各种可能的方案。This capability could be used to enable partial customization, local storage of information, using XAML to provide a business object, or a variety of possible scenarios. 这些方案的关键是 XamlReader 类及其 Load 方法。The key to these scenarios is the XamlReader class and its Load method. 输入是 XAML 文件,而输出是对象,该对象表示从该标记创建的整个运行时对象树。The input is a XAML file, and the output is an object that represents all of the run-time tree of objects that was created from that markup. 然后可以插入该对象,作为应用中已存在的另一个对象的属性。You then can insert the object to be a property of another object that already exists in the app. 只要该属性在内容模型中是一个合适的属性,而该内容模型具有最终显示功能并且将通知执行引擎已向应用添加新内容,就可以通过在 XAML 中进行加载轻松地修改正在运行的应用的内容。So long as the property is an appropriate property in the content model that has eventual display capabilities and that will notify the execution engine that new content has been added into the app, you can modify a running app's contents easily by loading in XAML. 对于 .NET Framework,通常只在完全受信任的应用程序中使用此功能,因为将文件加载到正在运行的应用程序中会带来明显的安全隐患。For .NET Framework, this capability is generally only available in full-trust applications, because of the obvious security implications of loading files into applications as they run.

请参阅See also