HOW TO:使用中繼資料存放區
更新:2007 年 11 月
當您使用擴充性自訂 Windows Presentation Foundation (WPF) Designer for Visual Studio 時,通常會建立自訂控制項。控制項的程式碼和定義控制項設計階段行為的中繼資料 (Metadata) 會納入不同的組件 (Assembly) 中。中繼資料會納入 MetadataStore。如需詳細資訊,請參閱 WPF 設計工具擴充性架構。
中繼資料存放區 (Metadata Store) 包含設計階段行為的資訊,例如自訂裝飾項、自訂內容功能表,以及自訂屬性編輯器。中繼資料存放區會實作為程式碼架構的屬性表格。
![]() |
---|
中繼資料組件的載入順序如下:先載入 *.Design.dll,再載入 *.VisualStudio.Design.dll 或 *.Expression.Design.dll。這可讓設計工具特定的中繼資料覆寫一般共用中繼資料。 |
將自訂屬性表格加入到中繼資料存放區
當設計工具載入自訂控制項時,它會在實作 IRegisterMetadata 的對應設計階段組件尋找型別。如果找到型別,它就會執行個體化該型別,並自動呼叫其 Register 方法。
若要將自訂屬性表格加入到中繼資料存放區
在控制項的一般設計階段組件 (<Your Control>.Design.dll) 中,加入名為 Metadata.cs 或 Metadata.vb 的檔案。
在 Metadata 檔案中,加入實作 IRegisterMetadata 的類別,然後實作 Register 方法。
具現化 AttributeTableBuilder 物件,然後呼叫 AddCustomAttributes 方法以便加入屬性。
呼叫 AddAttributeTable 方法,將 AttributeTable 加入到中繼資料存放區。
針對 Visual Studio 特定的設計階段組件 (<Your Control>.VisualStudio.Design.dll) 重複步驟 1 到 4。
範例
下列範例會加入自訂控制項的中繼資料。這段程式碼會將自訂屬性編輯器連接到自訂控制項的屬性。
internal class Metadata : Microsoft.Windows.Design.Metadata.IRegisterMetadata
{
public void Register()
{
Microsoft.Windows.Design.Metadata.AttributeTableBuilder builder = new Microsoft.Windows.Design.Metadata.AttributeTableBuilder();
//Property Editor
builder.AddCustomAttributes(typeof(<Your Custom Control>), <Property>, new System.ComponentModel.EditorAttribute(typeof(<Your Custom Editor>), typeof(<Your Custom Editor>)));
//Category Editor
builder.AddCustomAttributes(typeof(<Your Custom Control>), new System.ComponentModel.EditorAttribute(typeof(<Your Custom Editor>), typeof(<Your Custom Editor>)));
Microsoft.Windows.Design.Metadata.MetadataStore.AddAttributeTable(builder.CreateTable());
}
}
Friend Class Metadata
Implements Microsoft.Windows.Design.Metadata.IRegisterMetadata
Public Sub Register() Implements Microsoft.Windows.Design.Metadata.IRegisterMetadata.Register
Dim builder As New Microsoft.Windows.Design.Metadata.AttributeTableBuilder()
'Property Editor
builder.AddCustomAttributes(GetType(<Your Custom Control>), <Property>, New System.ComponentModel.EditorAttribute(GetType(<Your Custom Editor>), GetType(<Your Custom Editor>)))
'Category Editor
builder.AddCustomAttributes(GetType(<Your Custom Control>), New System.ComponentModel.EditorAttribute(GetType(<Your Custom Editor>), GetType(<Your Custom Editor>)))
Microsoft.Windows.Design.Metadata.MetadataStore.AddAttributeTable(builder.CreateTable())
End Sub
End Class
下列範例會加入自訂控制項的中繼資料。這段程式碼會將自訂裝飾項和自訂內容功能表連接到自訂控制項。
internal class Metadata : Microsoft.Windows.Design.Metadata.IRegisterMetadata
{
public void Register()
{
Microsoft.Windows.Design.Metadata.AttributeTableBuilder builder = new Microsoft.Windows.Design.Metadata.AttributeTableBuilder();
//Adorners
builder.AddCustomAttributes(typeof(<Your Custom Control>), new Microsoft.Windows.Design.Features.FeatureAttribute(typeof(<Your Custom Adorner Provider>)));
builder.AddCustomAttributes(typeof(<Your Custom Control>), new Microsoft.Windows.Design.Features.FeatureAttribute(typeof(<Your Custom Adorner Provider>)));
//MenuActions
builder.AddCustomAttributes(typeof(<Your Custom Control>), new Microsoft.Windows.Design.Features.FeatureAttribute(typeof(<Your Custom Context Menu Provider>)));
Microsoft.Windows.Design.Metadata.MetadataStore.AddAttributeTable(builder.CreateTable());
}
}
Friend Class Metadata
Implements Microsoft.Windows.Design.Metadata.IRegisterMetadata
Public Sub Register() Implements Microsoft.Windows.Design.Metadata.IRegisterMetadata.Register
Dim builder As New Microsoft.Windows.Design.Metadata.AttributeTableBuilder()
'Adorners
builder.AddCustomAttributes(GetType(<Your Custom Control>), New Microsoft.Windows.Design.Features.FeatureAttribute(GetType(<Your Custom Adorner Provider>)))
builder.AddCustomAttributes(GetType(<Your Custom Control>), New Microsoft.Windows.Design.Features.FeatureAttribute(GetType(<Your Custom Adorner Provider>)))
'MenuActions
builder.AddCustomAttributes(GetType(<Your Custom Control>), New Microsoft.Windows.Design.Features.FeatureAttribute(GetType(<Your Custom Context Menu Provider>)))
Microsoft.Windows.Design.Metadata.MetadataStore.AddAttributeTable(builder.CreateTable())
End Sub
End Class