共用方式為


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

執行緒安全

這個型別的任何 Public static (在 Visual Basic 中為 Shared) 成員都具備執行緒安全。並非所有的執行個體成員都是安全執行緒。

請參閱

參考

AttributeTable 成員

Microsoft.Windows.Design.Metadata 命名空間

AttributeTableBuilder

其他資源

中繼資料存放區