VS Addins Revisited: Declarative button order and location with a DSL
First, here is the code with the updates.
In the previous post I showed how you can declaratively create buttons with captions and icons in a visual studio addin. the next problem to solve in that space is how to you order them the way you want relative to each other?
A way I’ve come up with has a code based DSL that allows you to declare the location of each button type you have create like this:
public static ButtonLocationConfigurator LoadButtonConfiguration()
{
ButtonLocationConfigurator configurator = new ButtonLocationConfigurator();
configurator
.UnderCommandBar("MenuBar")
.UnderMenu("Test")
.PlaceButton<HelloWorldAddinAction>(ButtonVisuals.StartsNewGroup)
.PlaceButton<ActionThatReplacesItsIcon>();
return configurator;
}
The buttons should be placed correctly under the main menu bar under the test menu with the “HelloWorld” action above the other one.
Here is part of the code that makes this possible. first, what does the connect.cs look like?
public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
if(connectMode == ext_ConnectMode.ext_cm_UISetup)
{
CommandBars bars = (CommandBars)_applicationObject.CommandBars;
Commands2 commands = (Commands2)_applicationObject.Commands;
AddinCreatorContext creatorContext =
new AddinCreatorContext(_applicationObject, _addInInstance, commands, bars);
AddinRegistry.LoadButtonsByConfiguration(creatorContext);
}
}
This works really nicely and you can grab that code the does it here.