All Projects → madskristensen → OptionsSample

madskristensen / OptionsSample

Licence: other
A Visual Studio extension sample

Programming Languages

C#
18002 projects

Projects that are alternatives of or similar to OptionsSample

Unchase.OpenAPI.Connectedservice
📜 Visual Studio extension to generate OpenAPI (Swagger) web service reference.
Stars: ✭ 69 (+392.86%)
Mutual labels:  visual-studio, vsix, visual-studio-extension
Unchase.Odata.Connectedservice
📜 A Visual Studio extension for connecting to OData services with generating client-side C# proxy-classes
Stars: ✭ 39 (+178.57%)
Mutual labels:  visual-studio, vsix, visual-studio-extension
Community.VisualStudio.Toolkit
Making it easier to write Visual Studio extensions
Stars: ✭ 165 (+1078.57%)
Mutual labels:  visual-studio, vsix, visual-studio-extension
Codist
A visual studio extension which enhances syntax highlighting, quick info (tooltip), navigation bar, scrollbar, display quality and brings smart tool bar to code editor.
Stars: ✭ 134 (+857.14%)
Mutual labels:  visual-studio, vsix, visual-studio-extension
vs-material-icons-generator
This plugin will help you to set material design icons to your Xamarin projects In Visual Studio.
Stars: ✭ 50 (+257.14%)
Mutual labels:  visual-studio, visual-studio-extension
KnownMonikersExplorer
A Visual Studio extension
Stars: ✭ 21 (+50%)
Mutual labels:  vsix, visual-studio-extension
VS.DiffAllFiles
Visual Studio Extension to make comparing files before and after committing them to Git and TFS faster and easier.
Stars: ✭ 26 (+85.71%)
Mutual labels:  visual-studio, visual-studio-extension
ace-jump
Allows for quick movement around the editor screen. Inspired by AceJump for Webstorm and Vim plugin AceJump.
Stars: ✭ 44 (+214.29%)
Mutual labels:  visual-studio, visual-studio-extension
SmartCommandlineArgs
A Visual Studio Extension which aims to provide a better UI to manage your command line arguments
Stars: ✭ 70 (+400%)
Mutual labels:  visual-studio, visual-studio-extension
TextTemplatingCore
T4 Text Templating with .NET Core
Stars: ✭ 24 (+71.43%)
Mutual labels:  visual-studio, visual-studio-extension
rudebuild
A non-intrusive bulk/unity C++ build tool for Visual Studio
Stars: ✭ 16 (+14.29%)
Mutual labels:  visual-studio, visual-studio-extension
fsharp-linting-for-vs
Visual Studio Linter for F#
Stars: ✭ 33 (+135.71%)
Mutual labels:  visual-studio, vsix
unittestgenerator
A unit test generation extension for Visual Studio that aims to always produce code that compiles - covering the basic cases automatically and preparing as much as it can for the complex cases.
Stars: ✭ 32 (+128.57%)
Mutual labels:  visual-studio, visual-studio-extension
Dapr-Microservice-Template
A Microservice Template using Dapr.io and Kubernetes
Stars: ✭ 31 (+121.43%)
Mutual labels:  visual-studio, vsix
git-tools
This extension provides a git changes tool window, a graphical git history viewer and menus to launch Git Bash, Git Extenstions and TortoiseGit.
Stars: ✭ 72 (+414.29%)
Mutual labels:  visual-studio, visual-studio-extension
Cyotek.AddProjects
The Add Projects extension is a simple Visual Studio extension that makes it easy to add multiple projects to your solution.
Stars: ✭ 20 (+42.86%)
Mutual labels:  vsix, visual-studio-extension
vsixbootstrapper
An installer that can be chained with other packages to locate the latest VSIXInstaller.exe to use for installing VSIX extensions.
Stars: ✭ 19 (+35.71%)
Mutual labels:  visual-studio, vsix
PlantUml-Language-Service
PlantUml Language Service extension for Visual Studio 2017 and 2019
Stars: ✭ 24 (+71.43%)
Mutual labels:  visual-studio, visual-studio-extension
VerilogLanguageExtension
Verilog Language Extension for Visual Studio
Stars: ✭ 16 (+14.29%)
Mutual labels:  vsix, visual-studio-extension
XPathTools
A Visual Studio Extension which can run any XPath and XPath function; navigates through results at the click of a button. Can show and copy any XPath incl. XML namespaces, avoiding XML namespace induced headaches. Keeps track of the current XPath via the statusbar.
Stars: ✭ 40 (+185.71%)
Mutual labels:  visual-studio, visual-studio-extension

Options example

Build status

Applies to Visual Studio 2015 and newer

This sample shows how to correctly specify and consume options for a Visual Studio extension that is both thread-safe and performant.

The goal is to use best practices to achieve the following:

  • A simple way to provide custom options
  • Expose the options in the Tools -> Options dialog
  • Thread-safe way to access and modify the settings
  • Both synchronous and asyncronous support
  • No need to load the package for the settings to initialize

Let's get started

The source code in this sample adds two options pages to the Tools -> Options dialog - General and Other which is nested under the My Options node.

Options

To make them show up in that dialog they have to be registered on the package class itself using the ProvideOptionPage attribute, like so:

[ProvideOptionPage(typeof(DialogPageProvider.General), "My Options", "General", 0, 0, true)]
[ProvideOptionPage(typeof(DialogPageProvider.Other), "My Options", "Other", 0, 0, true)]
public sealed class MyPackage : AsyncPackage
{
    ...
}

Both GeneralOptions (source) and OtherOptions (source) are classes containing the settings as regular public properties. They are inheriting from the generic base class BaseOptionModel (source).

internal class GeneralOptions : BaseOptionModel<GeneralOptions>
{
    [Category("My category")]
    [DisplayName("Message box text")]
    [Description("Specifies the text to show in the message box")]
    [DefaultValue("My message")]
    public string Message { get; set; } = "My message";
}

These classes can be used from anywhere in the extension when options are needed, but we need to provide a way to expose them to VS and for that we're going to create a class called DialogPageProvider:

internal class DialogPageProvider
{
    public class General : BaseOptionPage<GeneralOptions> { }
    public class Other : BaseOptionPage<OtherOptions> { }
}

The classes specified inside the DialogPageProvider class are inheriting from the second generic base class in this sample - BaseOptionPage (source). It's sole responsibility is to function as the entry point for the Tools -> Options dialog.

That's it. We have now created custom options pages and registered them on the package.

Using the custom options

The options are ready to be consumed by our code and there are two ways to go about it:

1. From the UI thread

Whenever our code executes on the UI thread we can easily access the settings like so:

string message = GeneralOptions.Instance.Message;

This will throw when not on the UI thread so you'll catch any misuse during development.

2. From a background thread

This is a thread-safe way to obtain the options instance. When in doubt, use it this way.

GeneralOptions options = await GeneralOptions.GetLiveInstanceAsync();
string message = options.Message;

See how it is being used from the TextviewCreationListener.cs MEF component in the source code.

Modify the options

You can programmatically modify the options like this:

GeneralOptions options = await GeneralOptions.GetLiveInstanceAsync();
options.Message = "My new message";
await options.SaveAsync();

The above method can be called in a syncronous way on the UI thread, like so:

GeneralOptions.Instance.Message = "My new message";
GeneralOptions.Instance.Save();

It is recommented to do it async if possible.

There you have it. Custom options using best practices.

Futher reading

Read the docs for all the details surrounding these scenarios, but notice that while they do provide more detailed documentation, they don't follow the best practices outlined in this sample.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].