15

I have a simple Visual Studio extension that is built in a similar manner as the one presented in this walkthrough (using the IWpfTextViewCreationListener interface).

The extension uses two colors that I'd like to make configurable.

How can I define an Options Dialog for this extension? (for example, a properties page that would appear in the Tools/Options menu)

I have tried to do this using the DialogPage Class, but apparently it requires a VSPackage and I'm not sure if this approach is compatible with what I'm doing.

Cristian Lupascu
  • 39,078
  • 16
  • 100
  • 137
  • What do you means with VSPackage? Are you using add-ins? Mind that since VS2013 only Vsix extensions will be supported! – Kendar Dec 12 '13 at 17:43
  • @EDR by VSPackage I mean [this](http://msdn.microsoft.com/en-us/library/bb166424.aspx). This is a VS Extension whose code is [up on codeplex](https://netcodemetrics.codeplex.com/SourceControl/latest). I was thinking of adding a configuration dialog to make the colors configurable. – Cristian Lupascu Dec 12 '13 at 21:22

3 Answers3

2

I think you can make your colors customisable without providing a custom OptionsPage. You can Export your own colors and they will became configurable from Tools-Options-Fonts and Colors

By your linked example:

[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();
  }
}

Custom EFDs can provide SolidColorBrushes.

If this isn't enought, you can also access to the service provider used by VSPackages. You can make a package for the option page, and communicate with the Editor Extension through the service provider with a custom service.

You can import the service provider like this:

[Import]
internal SVsServiceProvider serviceProvider = null;

This soulution also doesn't require from you to migrate the original logic, only the creation of an additional package.

Ursegor
  • 878
  • 8
  • 16
1

I know this is old, but I thought some of these links might help you (NB: I haven't actually done this myself).

I'd guess you are missing attributes detailed in the MSDN Page:

MSDN - Walkthrough: Creating an Options Page

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

Some other options are:

Integrating into Visual Studio Settings

Registering Custom Options Pages

Obsidian Phoenix
  • 4,083
  • 1
  • 22
  • 60
  • thanks! I had actually visited these pages when I wanted to implement that feature, but they all revolve around using the DialogPage class, which seemed to not be usable unless I created a VSPackage, which was apparently incompatible with the way that project was built. – Cristian Lupascu Dec 19 '13 at 13:10
  • Hmm. Sounds like you would need to migrate the project into the newer style then. Sorry the links weren't more help. – Obsidian Phoenix Dec 19 '13 at 13:12
  • No problem. I cannot migrate to a newer structure, because it's somebody else's Open Source project and I only thought I would bring a small feature. I cannot afford to mess everything up. :) – Cristian Lupascu Dec 19 '13 at 15:03
1

If you do decide to go with making a VSPackage (as I believe that's the recommended approach), see this MSDN page and this other SO answer. I also show how to do this using C# and a WPF User Control for the Options Page on my blog here.

Community
  • 1
  • 1
deadlydog
  • 22,611
  • 14
  • 112
  • 118