Settings for your Visual Studio Extension

vs vsix dev encouragement 15 comments suggest edit

Recently I wrote what many consider to be the most important Visual Studio Extension ever shipped - Encourage for Visual Studio. It was my humble attempt to make a small corner of the world brighter with little encouragements as folks work in Visual Studio. You can get it via the Visual Studio Extension Manager.

But not everyone has a sunny disposition like I do. Some folks want to watch the world burn. What they want is Discouragements.

Well an idiot might write a whole other Visual Studio Extension with a set of discouragements. I may be many things, but I am no idiot. This problem is better solved by allowing users to configure the set of encouragements to be anything they want.

And that’s what I did. I added an Options pane to allow users to configure the set of encouragements. It turned out to be a more confusing ordeal than I expected. But with some help from Jared Parsons, I may now present to you, discouragements!

Encourage options

So if you’re of the masochistic inclination, you can treat yourself to custom discouragements all day long if you so choose.

Discouragement in use

As you can see from the screenshot, it supports native emoji!. If you want these for yourself, I posted them into a gist.

Challenges and Travails

So why was this challenging? Well like many things with development platforms, to do the basic thing is really easy, but when you want to deviate, things become hard.

If you follow the Walkthrough: Creating an Options Page you’ll be able to add settings to your Visual Studio extension pretty easily. Using this approach, you can even rely on Visual Studio to generate a properties UI for you.

basic options

But that’s pretty rudimentary.

What I wanted was very simple, I wanted a multi-line text box that let you type in or paste in an encouragement per line. So I derived from DialogPage as you do, created a WPF User Control with a TextBox. I added the user control to an ElementHost, a Windows Forms control that can host a WPF control because, apparently, the Options dialog is still hosting Windows Forms controls.

This approach was easy enough, but the text box didn’t accept any of my input. I ran into the same problem as this person writes about StackOverflow.

I could cut and paste into the TextBox, but I couldn’t type anything. That’s not very useful.

I wasn’t interested in overriding WndProc mainly because I feel I shouldn’t have to. Instead I gave up on WPF, and ported it over to a regular Windows Forms user control. That allowed me to type in the textbox, but if I hit the Enter key, instead of adding a newline, the OK button stole it. So I couldn’t actually add more than one encouragement.

UIElementDialogPage

Thankfully, Jared pointed me to the UIElementDialogPage.

If you want to provide a WPF User Control for your Visual Studio Extension, derive from UIElementDialogPage and not DialogPage like all the samples demonstrate!

It does all the necessary WndProc magic under the hood for you. Note that it was introduced in Visual Studio 2012 so if you take a dependency on it, your extension won’t work in Visual Studio 2010. Live in the present I always say.

Storing Settings

The other thing I learned is that AppSettings is not the place to save your extension’s settings. As Jared explained,

The use of application settings is not version safe in a VSIX. The location of the stored setting file path in part includes the version string and hashes of the executable. When Visual Studio installs an official update these values change and as a consequence change the setting file path. Visual Studio itself doesn’t support the use of application settings hence it makes no attempt to migrate this file to the new location and all information is essentially lost.

The supported method of settings is the WritableSettingsStore. It’s very similar to application settings and easy enough to access via SVsServiceProvider

public static WritableSettingsStore GetWritableSettingsStore(this SVsServiceProvider vsServiceProvider)
{
    var shellSettingsManager = new ShellSettingsManager(vsServiceProvider);
    return shellSettingsManager.GetWritableSettingsStore(SettingsScope.UserSettings);
}

If this is interesting to you, I encourage (tee hee) you to read through the Pull Request that adds settings to the Encourage pull request. You can read through the commits to watch me flailing around, or you can read the final DIFF to see what changes I had to make.

PS: If you liked this post follow me on Twitter for interesting links and my wild observations about pointless drivel

Found a typo or error? Suggest an edit! If accepted, your contribution is listed automatically here.

Comments

avatar

15 responses

  1. Avatar for distantcam
    distantcam July 31st, 2014

    In the first screenshot "Enter each encourament on its own line" should be encouragement.

  2. Avatar for Alister
    Alister July 31st, 2014

    How do you open this project anyway? I already tried with VS2010, VS2012, VS2013. No one seens to be capableoff open Encourage project.

  3. Avatar for Gene Reddick
    Gene Reddick July 31st, 2014

    Discouragements is a brilliant and valuable addition to my workflow. Thanks for nothing.

  4. Avatar for SimpleVar
    SimpleVar July 31st, 2014

    I myself prefer extensions that enhance productivity in one way or
    another, rather than attempt to alter my mood (I have music for that!).
    But there's still a lot to learn from this great post (which contains all the useful links, by the way - thanks ;3)

    But
    I really initially wanted to comment just to compliment on how great
    your blog looks, how neat the design is, and how I love the way you
    write. FUN

  5. Avatar for haacked
    haacked July 31st, 2014

    This is the best review of my work ever! Could you write it over here? http://visualstudiogallery....

  6. Avatar for haacked
    haacked July 31st, 2014

    Yeah, the real purpose of this extension is to teach myself how to write an extension. But if others have fun with it, all the better.

    > I really initially wanted to comment just to compliment on how great your blog looks, how neat the design is, and how I love the way you
    write. FUN

    Thanks!

  7. Avatar for Gene Reddick
    Gene Reddick July 31st, 2014

    Done.

  8. Avatar for Mykola Tarasyuk
    Mykola Tarasyuk July 31st, 2014

    The first time I read about your extension I found it not that useful for me. But the idea of discouragements definitely changes the situation :) Thanks a lot; will play with this today.

  9. Avatar for Matt
    Matt August 5th, 2014

    Would there be any way to save discouragement settings to a network share? In our team we like to insult each other in whimsical ways and adding to a global list to insult everyone in the team regularly would be a fine addition.

  10. Avatar for haacked
    haacked August 5th, 2014

    Log an issue here. https://github.com/haacked/...

  11. Avatar for AndreiV
    AndreiV August 26th, 2014

    I've tried adding special characters, such as "\n", but they are not being parsed. Is there a possibility to break one "encouragement" into multiple lines? Rick Rolling on a single line does not quite feel right... :)

  12. Avatar for Chris Bordeman
    Chris Bordeman June 6th, 2015

    Thanks, I've implemented Options based on what you've done here, with proper saving and reverting changes. My visual studio extension, MVVM Tools, is available as an example at https://github.com/cbordema.... It works for VS2010+.

  13. Avatar for Anant Anand Gupta
    Anant Anand Gupta June 3rd, 2016

    Hi there, It is great article.

    Can you please help me with the code where you have handled the multi-line issue with the OK button.

  14. Avatar for Philip
    Philip November 5th, 2016

    This was really useful for me. Thank you so much.

  15. Avatar for Greg Trevellick
    Greg Trevellick December 10th, 2016

    Great post, highly informative and clearly written.

    Love the vsix itself - okay it doesn't make me more productive per se, but when other devs are sat at my desk and see the encouragements, they become inquisitive about VS extensions, and potential future authors themselves.