ContentPropertyAttribute 类

定义

指示某种类型的哪个属性是 XAML 内容属性。 处理特性化类型的 XAML 表示形式的 XAML 子元素时,XAML 处理器会使用此信息。

public ref class ContentPropertyAttribute sealed : Attribute
/// [Windows.Foundation.Metadata.AttributeUsage(System.AttributeTargets.RuntimeClass)]
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
class ContentPropertyAttribute final : Attribute
/// [Windows.Foundation.Metadata.AttributeUsage(System.AttributeTargets.RuntimeClass)]
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.AttributeName("contentproperty")]
class ContentPropertyAttribute final : Attribute
[Windows.Foundation.Metadata.AttributeUsage(System.AttributeTargets.RuntimeClass)]
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
public sealed class ContentPropertyAttribute : Attribute
[Windows.Foundation.Metadata.AttributeUsage(System.AttributeTargets.RuntimeClass)]
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.AttributeName("contentproperty")]
public sealed class ContentPropertyAttribute : Attribute
Public NotInheritable Class ContentPropertyAttribute
Inherits Attribute
继承
ContentPropertyAttribute
属性

Windows 要求

设备系列
Windows 10 (在 10.0.10240.0 中引入)
API contract
Windows.Foundation.UniversalApiContract (在 v1.0 中引入)

注解

C++/WinRT。 请参阅 [contentproperty] 属性

此属性用于标识由 XAML 分析程序和其他 XAML 框架代码解释时,应将特性化类型的特定属性视为 XAML 内容属性。 XAML 内容属性的用途是,它提供一个 XAML 语法速记,可以省略该属性的属性元素标记。 删除属性元素有助于在 XAML 标记中实现更自然的父子形式。 有关 XAML 内容属性的详细信息,请参阅 XAML 语法指南的“XAML 内容属性”部分。

应用了 ContentPropertyAttribute 的默认Windows 运行时 XAML 词汇中的类的一个示例是 PanelPanel 上的属性 Children 被标识为由 ContentPropertyAttribute 及其 Name 值定义的 XAML 内容属性。 内容属性信息由所有派生类型的 Panel 继承,例如 GridCanvasStackPanel

此代码近似于 Panel 如何在 C# 中应用 ContentPropertyAttribute, (在本机代码中应用真实定义,此代码仅用于演示) :

[ContentProperty(Name = "Children")]
    public class Panel : FrameworkElement
    { ...}

使用 Panel 及其派生类 StackPanel 作为 XAML 内容属性概念的插图,你可能具有以下 XAML:

<StackPanel>
  <StackPanel.Children>
    <TextBlock>Testing content attribute</TextBlock>
  </StackPanel.Children>
</StackPanel>

上述内容等效于此更具可读性 XAML:

<StackPanel>
  <TextBlock>Testing content attribute</TextBlock>
</StackPanel>

当由 XAML 分析程序分析时,分析程序通过面板上应用的 ContentPropertyAttribute 知道,在 StackPanel 标记的正文中找到的任何内容都应用于在创建的 StackPanel 实例上设置 Children 的值。

TextBlock 类上可以看到另一个运行中的 ContentPropertyAttribute 的突出示例。 TextBlock 使用 Inlines 作为其内容属性,默认内联类 Run 使用 Text 作为其内容属性。 内容属性的组合可实现简单的内联语法,例如 <TextBlock>Hello</TextBlock>,即使此 XAML 创建的对象图更为复杂,并且还能够根据需要支持多个显式内联元素。

构造函数

ContentPropertyAttribute()

初始化 ContentPropertyAttribute 类的新实例。

字段

Name

指示某种类型的哪个属性是 XAML 内容属性。 处理特性化类型的 XAML 表示形式的 XAML 子元素时,XAML 处理器会使用此信息。

适用于

另请参阅