Visual Studio编辑器扩展选项对话框

Cri*_*scu 15 mef visual-studio-2010 visual-studio-extensions

我有一个简单的Visual Studio扩展,其构建方式与本演练中介绍的方式类似(使用IWpfTextViewCreationListener接口).

扩展使用了两种我想要配置的颜色.

如何为此扩展定义选项对话框?(例如,将显示在"工具"/"选项"菜单中的属性页)

我试图使用DialogPage类来做到这一点,但显然它需要一个VSPackage,我不确定这种方法是否与我正在做的兼容.

Urs*_*gor 2

我认为您可以在不提供自定义选项页面的情况下自定义颜色。您可以导出您自己的颜色,它们将变得可配置Tools- Options-Fonts and Colors

通过您链接的示例:

[Export(typeof(EditorFormatDefinition))]
[Name("EditorFormatDefinition/MyCustomFormatDefinition")]
[UserVisible(true)]
internal class CustomFormatDefinition : EditorFormatDefinition
{
  public CustomFormatDefinition( )
  {
    this.BackgroundColor = Colors.LightPink;
    this.ForegroundColor = Colors.DarkBlue;
    this.DisplayName = "My Cusotum Editor Format";
  }
}

[Export(typeof(EditorFormatDefinition))]
[Name("EditorFormatDefinition/MyCustomFormatDefinition2")]
[UserVisible(true)]
internal class CustomFormatDefinition2 : EditorFormatDefinition
{
  public CustomFormatDefinition2( )
  {
    this.BackgroundColor = Colors.DeepPink;
    this.ForegroundColor = Colors.DarkBlue;
    this.DisplayName = "My Cusotum Editor Format 2";
  }
}

[Export(typeof(IWpfTextViewCreationListener))]
[ContentType("text")]
[TextViewRole(PredefinedTextViewRoles.Document)]
internal class TestViewCreationListener : IWpfTextViewCreationListener
{
  [Import]
  internal IEditorFormatMapService FormatMapService = null;

  public void TextViewCreated( IWpfTextView textView )
  {
    IEditorFormatMap formatMap = FormatMapService.GetEditorFormatMap(textView);

    ResourceDictionary selectedText = formatMap.GetProperties("Selected Text");
    ResourceDictionary inactiveSelectedText = formatMap.GetProperties("Inactive Selected Text");

    ResourceDictionary myCustom = formatMap.GetProperties("EditorFormatDefinition/MyCustomFormatDefinition");
    ResourceDictionary myCustom2 = formatMap.GetProperties("EditorFormatDefinition/MyCustomFormatDefinition2");

    formatMap.BeginBatchUpdate();

    selectedText[EditorFormatDefinition.BackgroundBrushId] = myCustom[EditorFormatDefinition.BackgroundBrushId];
    formatMap.SetProperties("Selected Text", selectedText);

    inactiveSelectedText[EditorFormatDefinition.BackgroundBrushId] = myCustom2[EditorFormatDefinition.BackgroundBrushId];
    formatMap.SetProperties("Inactive Selected Text", myCustom2);

    formatMap.EndBatchUpdate();
  }
}
Run Code Online (Sandbox Code Playgroud)

定制 EFD 可以提供SolidColorBrushes。

如果这还不够,您还可以访问 VSPackages 使用的服务提供程序。您可以对选项页面进行封装,并通过服务提供者使用自定义服务与编辑器扩展进行通信。

您可以像这样导入服务提供商:

[Import]
internal SVsServiceProvider serviceProvider = null;
Run Code Online (Sandbox Code Playgroud)

该解决方案也不需要您迁移原始逻辑,只需要创建额外的包。