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 多載,將中繼資料加入至屬性表格產生器 (Builder)。完成加入中繼資料的動作後,您必須呼叫 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
執行緒安全
這個型別的任何 Public static (在 Visual Basic 中為 Shared) 成員都具備執行緒安全。並非所有的執行個體成員都是安全執行緒。
請參閱
參考
Microsoft.Windows.Design.Metadata 命名空間