將自訂屬性加入至圖層圖表

當您撰寫圖層的擴充程式碼在 Visual Studio Ultimate中時,您在分層圖可儲存任何項目的值。 當圖表儲存並重新開啟,值會保存。 您也可以讓這些屬性出現在 [屬性] 視窗中,好讓使用者可加以查看和編輯。 例如,您可以讓使用者為每個圖層指定規則運算式,並在每個圖層的依照使用者指定的樣式撰寫驗證程式碼驗證類別名稱。

使用者看不見的屬性

如果您想要程式碼附加至任何項目的值在分層圖,您不需要定義 MEF 元件。 取得 ILayerElement中的名為 Properties的資料夾 。 可將值加入至任何圖層項目字典。 做為分層圖時,它們會被儲存。 如需詳細資訊,請參閱巡覽及更新程式碼中的圖層模型

使用者可以編輯的屬性

初始準備

重要

若要讓屬性出現,您一定要讓圖層屬性是可見的每部電腦都執行下列變更。

  1. 使用 [以系統管理員身分執行],執行記事本。開啟 %ProgramFiles%\Microsoft Visual Studio 12.0\Common7\IDE\Extensions\Microsoft\Architecture Tools\ExtensibilityRuntime\extension.vsixmanifest

  2. 在 Content 項目內,加入:

    <MefComponent>Microsoft.VisualStudio.ArchitectureTools.Extensibility.Layer.Provider.dll</MefComponent>
  3. 在 Windows 開始功能表,請在 [Microsoft Visual Studio 2012] 底下,按一下 [Visual Studio 工具],開啟 [開發人員命令提示字元]。

    輸入:

    devenv /rootSuffix /updateConfiguration

    devenv /rootSuffix Exp /updateConfiguration

  4. 重新啟動 Visual Studio。

請確定您在 VSIX 專案的程式碼

如果您的屬性是命令、筆勢或驗證專案的一部分,您不需要加入任何項目。 在當做 MEF 元件中定義的 Visual Studio 擴充性專案應該定義您的自訂屬性的程式碼。 如需詳細資訊,請參閱 在圖層圖表中加入命令和軌跡在圖層圖表中加入自訂架構驗證

定義自訂屬性

若要建立自訂屬性,請定義如下的類別:

[Export(typeof(IPropertyExtension))]
public class MyProperty 
      : PropertyExtension<ILayerElement>
{
  // Implement the interface.
}

您可以定義在 ILayerElement 的屬性或其任何衍生類別,包括:

  • ILayerModel - 模型

  • ILayer - 每一個圖層

  • ILayerDependencyLink - 圖層之間的連結

  • ILayerComment

  • ILayerCommentLink

若要查看您的自訂屬性

重要

只有架構總管在載入模型專案前開啟,自訂屬性才會出現。您可能必須開啟架構總管然後停止並重新啟動 Visual Studio 才能看到自訂屬性。在 [架構] 功能表上,選擇 [Windows]、[架構總管]。

若要測試您的自訂屬性,請按 F5 以啟動 Visual Studio 的實驗執行個體。 建立適當的圖層項目的範例,並加以選取。 您會看到在屬性視窗的自訂屬性。

範例

下列程式碼是典型的自訂屬性描述元。 它會在圖層模型 (ILayerModel) 上定義布林值屬性,好讓使用者為自訂驗證方法提供值。

using System;
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.ArchitectureTools.Extensibility.Layer;

namespace MyNamespace
{
  /// <summary>
  /// Custom properties are added to the Layer Designer via a custom
  /// Property Descriptor. We have to export this Property Descriptor
  /// using MEF to make it available in the Layer Designer.
  /// </summary>
  [Export(typeof(IPropertyExtension))]
  public class AllTypesMustBeReferencedProperty 
      : PropertyExtension<ILayerModel>
  {
    /// <summary>
    /// Each custom property must have a unique name. 
    /// Usually we use the full name of this class.
    /// </summary>
    public static readonly string FullName =
      typeof(AllTypesMustBeReferencedProperty).FullName;

    /// <summary>
    /// Construct the property. Notice the use of FullName.
    /// </summary>
    public AllTypesMustBeReferencedProperty()
            : base(FullName)
    {  }

    /// <summary>
    /// The display name is shown in the Properties window.
    /// We therefore use a localizable resource.
    /// </summary>
    public override string DisplayName
    {
      get { return Strings.AllTypesMustBeReferencedDisplayName; }
    }

    /// <summary>
    /// Description shown at the bottom of the Properties window.
    /// We use a resource string for easier localization.
    /// </summary>
    public override string Description
    {
      get { return Strings.AllTypesMustBeReferencedDescription; }
    }

    /// <summary>
    /// This is called to set a new value for this property. We must
    /// throw an exception if the value is invalid.
    /// </summary>
    /// <param name="component">The target ILayerElement</param>
    /// <param name="value">The new value</param>
    public override void SetValue(object component, object value)
    {
      ValidateValue(value);
      base.SetValue(component, value);
    }
    /// <summary>
    /// Helper to validate the value.
    /// </summary>
    /// <param name="value">The value to validate</param>
    private static void ValidateValue(object value)
    {  }

    public override Type PropertyType
    { get { return typeof(bool); } }

    /// <summary>
    /// The segment label of the properties window.
    /// </summary>
    public override string Category
    { 
      get
      {
        return Strings.AllTypesMustBeReferencedCategory;
      }
    }
  }
}

請參閱

概念

擴充圖層圖表