AttributeCallbackBuilder 类

更新:2007 年 11 月

此类的一个实例被传递给回调委托,从而以闲散方式填充类型的属性。

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

语法

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

备注

使用 AttributeCallbackBuilder 可以按需填充 AttributeTable。这对于存放大量属性的属性表非常重要。如果只为某一类型指定少数几个属性,请单独使用 AttributeTableBuilder

使用委托格式时,在设计器请求目标类型的元数据之前,不会构造任何属性。AttributeTableBuilder 会将此回调信息传输到 AttributeTable 中并进行保留。因此,这些回调委托只按需调用。一旦调用,它们的结果便由 AttributeTable 进行缓存。

通过使用 AddCallback 方法按需启用属性创建。

示例

下面的代码示例演示如何使用 AttributeCallbackBuilder 类创建和填充属性表。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Reflection;
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();

            // Build the attribute table by using the AttributeCallbackBuilder 
            // class. The attribute table is not populated until the designer
            // needs it, which is more efficient for large attribute tables.
            builder.AddCallback(
                typeof(Button), 
                delegate(AttributeCallbackBuilder callbackBuilder)
            {
                callbackBuilder.AddCustomAttributes(
                    new DefaultPropertyAttribute("Content"));

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

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

                callbackBuilder.AddCustomAttributes(
                   Button.WidthProperty,
                   new TypeConverterAttribute(typeof(LengthConverter)),
                   new DescriptionAttribute("This is the width"));

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

            MetadataStore.AddAttributeTable(builder.CreateTable());
        }
    }
}

继承层次结构

System.Object
  Microsoft.Windows.Design.Metadata.AttributeCallbackBuilder

线程安全

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

另请参见

参考

AttributeCallbackBuilder 成员

Microsoft.Windows.Design.Metadata 命名空间

AttributeTableBuilder

AddCallback

AttributeTable

其他资源

元数据存储区

演练:创建设计时装饰器