![]() |
||||
|
This article describes why commands created by Visual Studio .NET add-ins disappear on the next session and how to restore them. More Information Using the wizard provided by Visual Studio .NET to create add-ins ("Other Projects", "Extensibility Projects", "Visual Studio .NET Add-in" project kind) the 4th step allows you to create a "Tools" menu item through the checkbox under the question "Would you like to create UI for the user to interact with your Add-in?". Selecting this checkbox will cause the following code to appear in your OnConnection method:
Public Sub OnConnection(ByVal application As Object, _
ByVal connectMode As Extensibility.ext_ConnectMode, _
ByVal addInInst As Object, ByRef custom As System.Array) _
Implements Extensibility.IDTExtensibility2.OnConnection
applicationObject = CType(application, EnvDTE.DTE)
addInInstance = CType(addInInst, EnvDTE.AddIn)
If connectMode = Extensibility.ext_ConnectMode.ext_cm_UISetup Then
Dim objAddIn As AddIn = CType(addInInst, AddIn)
Dim CommandObj As Command
Try
CommandObj = applicationObject.Commands.AddNamedCommand(objAddIn, "MyAddin15", _
"MyAddin15", "Executes the command for MyAddin15", True, 59, Nothing, 1 + 2)
'1+2 == vsCommandStatusSupported+vsCommandStatusEnabled
CommandObj.AddControl(applicationObject.CommandBars.Item("Tools"))
Catch e as System.Exception
End Try
End If
End Sub
public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode,
object addInInst, ref System.Array custom)
{
applicationObject = (_DTE)application;
addInInstance = (AddIn)addInInst;
if(connectMode == Extensibility.ext_ConnectMode.ext_cm_UISetup)
{
object []contextGUIDS = new object[] { };
Commands commands = applicationObject.Commands;
_CommandBars commandBars = applicationObject.CommandBars;
try
{
Command command = commands.AddNamedCommand(addInInstance, "MyAddin1", "MyAddin1",
"Executes the command for MyAddin1", true, 59, ref contextGUIDS,
(int)vsCommandStatus.vsCommandStatusSupported+(int)vsCommandStatus.vsCommandStatusEnabled);
CommandBar commandBar = (CommandBar)commandBars["Tools"];
CommandBarControl commandBarControl = command.AddControl(commandBar, 1);
}
catch(System.Exception /*e*/)
{
}
}
}
As you can see, the command is created only when the connectMode parameter value is ext_cm_UISetup, and that happens only once in the whole life of the add-in after installed on a computer: when the add-in is loaded for the first time for the current user. This is convenient because add-ins should not create commands on startup and destroy them on shutdown. Rather, they should be created only once to load faster and to preserve keyboard bindings created by the user for those commands. Visual Studio .NET knows if it is the first time that the add-in is loaded (and therefore if it must pass the value ext_cm_UISetup in the connectMode parameter) through a Windows Registry key, as explained in the article HOWTO: Reset a Visual Studio add-in. When Visual Studio .NET is closed, it persists add-in command information on disk. That information is stored for each user in the following file:
where <version> is:
This persistence mechanism causes the following issue when you debug an add-in for the first time:
There are several ways to solve this problem, which are detailed here:
Note: Visual Studio 2005 and higher doesn't exhibit this problem because it introduced a new command-line switch /resetaddin <addin_namespace.Connect> for devenv.exe to reset those registry entries for the add-in, and the add-in project generated by the add-in wizard uses that switch in the property pages of the add-in project ("Debug" tab, "Start Options" section, "Command line arguments" field) to reset the add-in each time you debug it. This could lead to the false conclusion that ext_ConnectMode.ext_cm_UISetup is fired each time that the add-in runs. It is not. It only happens when you debug add-ins with Visual Studio 2005 or higher. It doesn't happen with Visual Studio .NET 2002/2003, it doesn't happen when the add-in is used on a final machine (without debugging) and if you remove the /resetaddin flag of the property page of your Visual Studio 2005 project, it won't happen too when you debug your add-in. Related articles
Go back to the 'Resources for Visual Studio .NET extensibility' section for more articles like this
You can code, design, locate code and document your apps much faster using VB.NET, C#, C++ or Visual J#:
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||