将按钮添加到Visual Studio卸载的项目上下文菜单(找不到CommandBar的名称)

时间:2012-01-03 23:12:06

标签: visual-studio-2010 visual-studio-addins

NuGet包恢复与PostSharp软件包不兼容,这对我来说非常烦恼,手动编辑csproj文件很快就会变老。

我想创建一个小的Visual Studio AddIn,通过在卸载的项目上下文菜单中添加一个额外的按钮来修复项目。

我遇到的问题听起来很简单,但却困扰着我:我不知道怎样找不到已卸载的项目上下文菜单的CommandBar。

因此,总结了一个问题:卸载的项目上下文菜单的CommandBar名称是什么。

非常感谢!

1 个答案:

答案 0 :(得分:1)

显然:“Stub Project”。在列表中找到:CommandBar names

总结OnConnection方法来注册它:

    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";

            //Place the command on the tools menu.
            //Find the MenuBar command bar, which is the top-level command bar holding all the main menu items:
            Microsoft.VisualStudio.CommandBars.CommandBar menuBarCommandBar = ((Microsoft.VisualStudio.CommandBars.CommandBars)_applicationObject.CommandBars)["MenuBar"];

            //Find the Tools command bar on the MenuBar command bar:
            CommandBarControl toolsControl = menuBarCommandBar.Controls[toolsMenuName];
            CommandBars cmdBars = (CommandBars)(_applicationObject.CommandBars);
            CommandBar vsBarProject = cmdBars["Stub Project"];
            CommandBarPopup toolsPopup = (CommandBarPopup)toolsControl;

            //This try/catch block can be duplicated if you wish to add multiple commands to be handled by your Add-in,
            //  just make sure you also update the QueryStatus/Exec method to include the new command names.
            try
            {
                //Add a command to the Commands collection:
                Command command = commands.AddNamedCommand2(_addInInstance, "FixNuGetPostSharp", "Fix NuGet PostSharp", "Executes the command for VsextensionTest", true, 0, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);

                //Add a control for the command to the tools menu:
                if ((command != null) && (toolsPopup != null))
                {
                    command.AddControl(vsBarProject);
                }
            }
            catch (System.ArgumentException)
            {
                //If we are here, then the exception is probably because a command with that name
                //  already exists. If so there is no need to recreate the command and we can 
                //  safely ignore the exception.
            }
        }
    }