【问题标题】:Visual Studio Editor Extension Options DialogVisual Studio 编辑器扩展选项对话框
【发布时间】:2012-07-11 02:46:44
【问题描述】:

我有一个简单的 Visual Studio 扩展,它的构建方式与 this walkthrough 中提供的类似(使用 IWpfTextViewCreationListener 接口)。

该扩展程序使用了两种我希望可配置的颜色。

如何为这个扩展定义一个选项对话框? (例如,出现在“工具/选项”菜单中的属性页面)

我曾尝试使用DialogPage Class 执行此操作,但显然它需要一个 VSPackage,我不确定这种方法是否与我正在做的兼容。

【问题讨论】:

  • VSPackage 是什么意思?你在使用插件吗?请注意,自 VS2013 起,仅支持 Vsix 扩展!
  • VSPackage 的@EDR 我的意思是this。这是一个代码为up on codeplex 的VS 扩展。我正在考虑添加一个配置对话框以使颜色可配置。

标签: visual-studio-2010 mef visual-studio-extensions


【解决方案1】:

我认为您可以在不提供自定义选项页面的情况下自定义颜色。 您可以导出自己的颜色,它们可以从 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();
  }
}

自定义 EFD 可以提供SolidColorBrushes。

如果这还不够,您还可以访问 VSPackages 使用的服务提供程序。您可以为选项页面制作一个包,并通过服务提供者使用自定义服务与Editor Extension进行通信。

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

[Import]
internal SVsServiceProvider serviceProvider = null;

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

【讨论】:

    【解决方案2】:

    我知道这是旧的,但我认为其中一些链接可能会对您有所帮助(注意:我自己实际上并没有这样做)。

    我猜你缺少 MSDN 页面中详述的属性:

    MSDN - Walkthrough: Creating an Options Page

    [ProvideOptionPage(typeof(OptionPageGrid),
    "My Category", "My Grid Page", 0, 0, true)]
    

    其他一些选项是:

    Integrating into Visual Studio Settings

    Registering Custom Options Pages

    【讨论】:

    • 谢谢!当我想实现该功能时,我实际上已经访问了这些页面,但是它们都围绕着使用 DialogPage 类,除非我创建了一个 VSPackage,否则它似乎无法使用,这显然与该项目的构建方式不兼容。跨度>
    • 嗯。听起来您需要将项目迁移到更新的样式中。抱歉,链接没有更多帮助。
    • 没问题。我无法迁移到更新的结构,因为它是其他人的开源项目,我只认为我会带来一个小功能。我不能把一切都搞砸。 :)
    【解决方案3】:

    如果您决定制作 VSPackage(我相信这是推荐的方法),请参阅 this MSDN page 和其他 SO answer。我还展示了如何使用 C# 和用于选项页 on my blog here 的 WPF 用户控件来执行此操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-08
      • 1970-01-01
      • 1970-01-01
      • 2012-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多