共用方式為


提供設計階段中繼資料

在為 WPF 或 Silverlight 控制項撰寫自訂設計經驗時,您將它們部署在個別的設計階段組件中。 您透過建立以中繼資料屬性填入的資料表,指定這些自訂設計階段實作與 Visual Studio 和 Expression Blend 等工具的互動方式。

設計階段組件

當設計工具 (例如 Visual Studio) 開啟自訂控制項的組件時,也會尋找相關的設計階段組件。 特別是,設計工具會尋找 ProvideMetadataAttribute 組件層級屬性。 在找到這個屬性後,設計工具便會在組件中搜尋實作 IProvideAttributeTable 介面的類別。 設計工具會查詢這個類別的 AttributeTable 屬性 (Property),找出指定設計階段行為的屬性 (Attribute) 集合。 如需詳細資訊,請參閱逐步解說:提供自訂設計階段中繼資料部署自訂控制項和設計階段屬性

屬性表格

AttributeTable 類別會讓設計階段的中繼資料屬性與 WPF 和 Silverlight 型別產生關聯。 屬性表格會針對可設計型別,指定特定的設計階段實作。 例如,如果您的控制項具有自訂 AdornerPanel,您可將 FeatureAttribute 加入至屬性表格,以指定自訂 FeatureProvider 實作。

屬性表格產生器

若要建立屬性表格,必須從建立 AttributeTableBuilder 類別的執行個體開始。 接著再呼叫 AddCustomAttributes 方法,將中繼資料加入至屬性表格產生器。 完成加入中繼資料後,呼叫 CreateTable 方法來產生屬性表格。 屬性表格產生器方法支援回呼委派,因此會延後建立屬性表格,直到有需求為止。

下列程式碼示範如何使用屬性表格,指定自訂裝飾項實作。

' Container for any general design-time metadata to initialize.
' Designers look for a type in the design-time assembly that 
' implements IProvideAttributeTable. If found, designers instantiate
' this class and access its AttributeTable property automatically.
Friend Class Metadata
    Implements IProvideAttributeTable

    ' Accessed by the designer to register any design-time metadata.
    Public ReadOnly Property AttributeTable() As AttributeTable _
        Implements IProvideAttributeTable.AttributeTable
        Get
            Dim builder As New AttributeTableBuilder()

            ' Add the adorner provider to the design-time metadata.
            builder.AddCustomAttributes(GetType(ButtonWithDesignTime), _
                                        New FeatureAttribute(GetType(OpacitySliderAdornerProvider)))

            Return builder.CreateTable()
        End Get
    End Property


End Class
// Container for any general design-time metadata to initialize.
// Designers look for a type in the design-time assembly that 
// implements IProvideAttributeTable. If found, designers instantiate 
// this class and access its AttributeTable property automatically.
internal class Metadata : IProvideAttributeTable
{
    // Accessed by the designer to register any design-time metadata.
    public AttributeTable AttributeTable
    {
        get 
        {
            AttributeTableBuilder builder = new AttributeTableBuilder();

            // Add the adorner provider to the design-time metadata.
            builder.AddCustomAttributes(
                typeof(ButtonWithDesignTime),
                new FeatureAttribute(typeof(OpacitySliderAdornerProvider)));

            return builder.CreateTable();
        }
    }
}

如需詳細資訊,請參閱逐步解說:建立設計階段裝飾項

基本設計階段實作工作流程

在撰寫自訂設計經驗時,您通常會遵照類似的工作流程。 如需詳細資訊,請參閱 HOW TO:部署自訂控制項和設計階段屬性

如需示範如何實作自訂設計階段經驗的範例,請參閱 WPF 和 Silverlight Designer 擴充性範例 (英文)。

個別設計階段組件的優點

WPF Designer for Visual Studio 架構將設計階段中繼資料與實作分開處理。 將中繼資料與控制項的執行階段程式碼分開處理是一項重要的設計原則,原因如下。

  • 在小組之間建置返回和整合邏輯,可能會使中繼資料編譯成控制項的工作變得十分麻煩。

  • 將中繼資料編譯成控制項後,外部工具 (例如 WPF 設計工具或 Expression Blend) 稍後就無法修改這些中繼資料。 這會嚴重影響靈活度。 若未將設計階段中繼資料與控制項分開處理,Visual Studio 就需要新版 .NET Framework,才能建立設計工具的版本。

  • 將中繼資料編譯成控制項會大幅增加控制項組件的大小。 設計階段屬性也會拖慢控制項的速度。 將其他屬性載入記憶體時,使用反映的控制項功能 (例如資料繫結) 會受到影響。

  • 設計階段中繼資料能提供設計工具的「特質」。 設計工具的功能大部分都會繫結到裝載它的應用程式,而非控制項。 WPF 設計工具和 Expression Blend 使用不同的中繼資料集,針對特定類型的使用者提供功能集。

請參閱

參考

AttributeTable

ProvideMetadataAttribute

其他資源

HOW TO:部署自訂控制項和設計階段屬性

部署自訂控制項和設計階段屬性

基本擴充性概念

了解 WPF 設計工具擴充性