AttributeTableBuilder 类

更新:2007 年 11 月

创建可传递到元数据存储区中的属性表。

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

语法

声明
Public Class AttributeTableBuilder
用法
Dim instance As AttributeTableBuilder
public class AttributeTableBuilder
public ref class AttributeTableBuilder
public class AttributeTableBuilder

备注

若要创建属性表,需要先创建 AttributeTableBuilder 类的一个实例。通过调用 AddCustomAttributes 重载将元数据添加到属性表生成器中。添加完元数据后,通过调用 CreateTable 方法从属性表生成器中生成一个属性表。属性表生成器方法支持回调委托,因此可将属性表的创建一直推迟到需要时。

如果要添加多个属性,请使用 AttributeCallbackBuilder 类来代替 AttributeTableBuilder 类。此方法会将属性的创建一直推迟到设计器为目标类型请求元数据时。

示例

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

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.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()

        builder.AddCustomAttributes( _
            GetType(Button), _
            New DefaultPropertyAttribute("Content"))

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

        Dim properties As PropertyDescriptorCollection = _
            TypeDescriptor.GetProperties(GetType(Button))
        Dim pd As PropertyDescriptor = properties("Foreground")
        builder.AddCustomAttributes( _
            GetType(Button), _
            pd, _
            New ReadOnlyAttribute(True))

        builder.AddCustomAttributes( _
            GetType(Button), _
            Button.WidthProperty, _
            New ReadOnlyAttribute(True))

        Dim members As MemberInfo() = GetType(Button).GetMember("Height")
        builder.AddCustomAttributes( _
            GetType(Button), _
            members(0), _
            New ReadOnlyAttribute(True))

        Dim attributes As AttributeTable = builder.CreateTable()

        MetadataStore.AddAttributeTable(attributes)

    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();

            builder.AddCustomAttributes(
                typeof(Button),
                new DefaultPropertyAttribute("Content"));

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

            PropertyDescriptorCollection properties =
                TypeDescriptor.GetProperties(typeof(Button));
            PropertyDescriptor pd = properties["Foreground"];
            builder.AddCustomAttributes( 
                typeof(Button), 
                pd,
                new ReadOnlyAttribute(true)); 

            builder.AddCustomAttributes(
                typeof(Button),
                Button.WidthProperty,
                new ReadOnlyAttribute(true)); 

            MemberInfo[] members = typeof(Button).GetMember("Height");
            builder.AddCustomAttributes(
                typeof(Button),
                members[0],
                new ReadOnlyAttribute(true)); 

            AttributeTable attributes = builder.CreateTable();

            MetadataStore.AddAttributeTable(attributes);
        }
    }
}

继承层次结构

System.Object
  Microsoft.Windows.Design.Metadata.AttributeTableBuilder

线程安全

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

另请参见

参考

AttributeTableBuilder 成员

Microsoft.Windows.Design.Metadata 命名空间

AttributeTable

AttributeCallbackBuilder

其他资源

元数据存储区