Chapter 13: Extending Visual Studio 2010 385

Reviewing OnConnection Generated Code

You know that the purpose of the OnConnection method is to help initialize the Add-In, and

you’ve seen the parameters populated by VS and what each parameter means. Listing 13-2

shows the code generated by VS after the Add-In Project Wizard completes. It reflects the

result of choosing to have a command bar UI, shown in Figure 13-5. Code comments were

omitted to place more focus on the code itself.

Listing 13-2 The OnConnection method
C#:
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)
{
object []contextGUIDS = new object[] { };
Commands2 commands =
(Commands2)_applicationObject.Commands;
string toolsMenuName = "Tools";
Microsoft.VisualStudio.CommandBars.CommandBar
menuBarCommandBar = ((
Microsoft.VisualStudio.CommandBars.CommandBars)
_applicationObject.CommandBars)["MenuBar"];
CommandBarControl toolsControl =
menuBarCommandBar.Controls[toolsMenuName];
CommandBarPopup toolsPopup =
(CommandBarPopup)toolsControl;
try
{
Command command = commands.AddNamedCommand2(
_addInInstance, "KeystrokeFinder",
"KeystrokeFinder",
"Executes the command for KeystrokeFinder",
true, 59, ref contextGUIDS,
(int)vsCommandStatus
.vsCommandStatusSupported+
(int)vsCommandStatus.vsCommandStatusEnabled,