我正在使用此代码将项目添加到代码窗口右键菜单:
public void OnConnection(
object application,
ext_ConnectMode connectMode,
object addInInst,
ref Array custom)
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
object[] contextGUIDS = new object[] { };
Command codeWindowCommand = null;
CommandBarControl codeWindowButton;
CommandBar codeCommandBar;
CommandBars commandBars;
try
{
codeWindowCommand = _applicationObject.Commands.Item(
_addInInstance.ProgID + "." + CODEWINDOW_COMMAND_NAME, 0);
}
catch
{
}
if (codeWindowCommand == null)
{
codeWindowCommand = _applicationObject.Commands.AddNamedCommand(
_addInInstance,
CODEWINDOW_COMMAND_NAME,
CODEWINDOW_COMMAND_NAME,
"Pastebin selected code",
true,
18,
ref contextGUIDS,
(int)vsCommandStatus.vsCommandStatusSupported +
(int)vsCommandStatus.vsCommandStatusEnabled);
}
commandBars = (CommandBars)_applicationObject.CommandBars;
codeCommandBar = commandBars["Code Window"];
codeWindowButton = (CommandBarControl)codeWindowCommand.AddControl(
codeCommandBar, codeCommandBar.Controls.Count + 1);
codeWindowButton.Caption = "Text for button";
codeWindowButton.TooltipText = "Tooltip for button";
}
并且插件设置为自动启动。然而,每次运行 VS2008 时,它都会在菜单中添加另一个按钮,直到我完全删除插件。有谁知道我如何解决这个问题?
例如,我会将 Command.AddControl() 和稍后的内容包装在 if 中,如果仅在按钮不存在时执行,但我似乎找不到在 API 中检查它的方法?