AttributeTable 类

更新:2007 年 11 月

用于定义设计时外观和行为的元数据属性表。

命名空间:  Microsoft.Windows.Design.Metadata
程序集:  Microsoft.Windows.Design(在 Microsoft.Windows.Design.dll 中)

语法

声明
Public NotInheritable Class AttributeTable
用法
Dim instance As AttributeTable
public sealed class AttributeTable
public ref class AttributeTable sealed
public final class AttributeTable

备注

使用 AttributeTable 类可以使设计时元数据属性与 Windows Presentation Foundation (WPF) 类型关联。

属性表实际上是一个只读字典,但其键和值分别进行计算。如果属性表中包含特定类型的属性,查询属性表将很有效。实际属性集是按需创建的。

若要创建属性表,请调用 AttributeTableBuilder 类的 CreateTable 方法。

示例

下面的代码示例演示如何创建和填充属性表。有关更多信息,请参见演练:创建设计时装饰器

Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Reflection
Imports System.Text
Imports System.Windows.Media
Imports System.Windows.Controls
Imports System.Windows

Imports Microsoft.Windows.Design
Imports Microsoft.Windows.Design.Features
Imports Microsoft.Windows.Design.Metadata

' Container for any general design-time metadata to initialize.
' Designers look for a type in the design-time assembly that 
' implements IRegisterMetadata. If found, designers instantiate 
' this class and call its Register() method automatically.

Friend Class Metadata
    Implements IRegisterMetadata

    ' Called by the designer to register any design-time metadata.
    Public Sub Register() Implements IRegisterMetadata.Register
        Dim builder As New AttributeTableBuilder()

        ' Apply the ReadOnlyAttribute to the Background property 
        ' of the Button class.
        builder.AddCustomAttributes(GetType(Button), "Background", New ReadOnlyAttribute(True))

        Dim attributes As AttributeTable = builder.CreateTable()

        MetadataStore.AddAttributeTable(attributes)

        Dim hasCustomAttributes As Boolean = attributes.ContainsAttributes(GetType(Button))

        Dim types As IEnumerable(Of Type) = attributes.AttributedTypes

        ' The following code shows how to retrieve custom attributes
        ' using the GetCustomAttributes method overloads.

        Dim attrs0 As IEnumerable = attributes.GetCustomAttributes(GetType(Button))

        Dim attrs1 As IEnumerable = attributes.GetCustomAttributes(GetType(Button), "Background")

        Dim properties As PropertyDescriptorCollection = TypeDescriptor.GetProperties(GetType(Button))
        Dim pd As PropertyDescriptor = properties("Background")
        Dim attrs2 As IEnumerable = attributes.GetCustomAttributes(GetType(Button), pd)

        Dim attrs3 As IEnumerable = attributes.GetCustomAttributes(GetType(Button), Button.BackgroundProperty)

        Dim members As MemberInfo() = GetType(Button).GetMembers()
        Dim i As Integer
        For i = 0 To members.Length
            If members(i).Name = "Background" Then
                Dim attrs4 As IEnumerable = attributes.GetCustomAttributes(GetType(Button), members(i))
            End If
        Next i

    End Sub

End Class
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using System.Text;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows;

using Microsoft.Windows.Design.Features;
using Microsoft.Windows.Design.Metadata;

namespace CustomControlLibrary.VisualStudio.Design
{
    // Container for any general design-time metadata to initialize.
    // Designers look for a type in the design-time assembly that 
    // implements IRegisterMetadata. If found, designers instantiate 
    // this class and call its Register() method automatically.
    internal class Metadata : IRegisterMetadata
    {
        // Called by the designer to register any design-time metadata.
        public void Register()
        {
            AttributeTableBuilder builder = new AttributeTableBuilder();

            // Apply the ReadOnlyAttribute to the Background property 
            // of the Button class.
            builder.AddCustomAttributes(
                typeof(Button),
                "Background",
                new ReadOnlyAttribute(true)  );

            AttributeTable attributes = builder.CreateTable();

            MetadataStore.AddAttributeTable(attributes);

            bool hasCustomAttributes = attributes.ContainsAttributes(typeof(Button));

            IEnumerable<Type> types = attributes.AttributedTypes;

            // The following code shows how to retrieve custom attributes
            // using the GetCustomAttributes method overloads.

            IEnumerable attrs0 = attributes.GetCustomAttributes(typeof(Button));

            IEnumerable attrs1 = attributes.GetCustomAttributes(
                typeof(Button), 
                "Background");

            PropertyDescriptorCollection properties = 
                TypeDescriptor.GetProperties(typeof(Button));
            PropertyDescriptor pd = properties["Background"];
            IEnumerable attrs2 = attributes.GetCustomAttributes(typeof(Button), pd);

            IEnumerable attrs3 = attributes.GetCustomAttributes(
                typeof(Button),
                Button.BackgroundProperty);

            MemberInfo[] members = typeof(Button).GetMembers();
            for (int i = 0; i < members.Length; i++)
            {
                if (members[i].Name == "Background")
                {
                    IEnumerable attrs4 = attributes.GetCustomAttributes(
                        typeof(Button), 
                        members[i]);
                }
            }
        }
    }
}

继承层次结构

System.Object
  Microsoft.Windows.Design.Metadata.AttributeTable

线程安全

此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。

另请参见

参考

AttributeTable 成员

Microsoft.Windows.Design.Metadata 命名空间

AttributeTableBuilder

其他资源

元数据存储区