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

发布于 2024-12-24 23:34:16 字数 275 浏览 1 评论 0原文

NuGet 包恢复不能与 PostSharp 包很好地配合,这对我来说非常烦恼,并且手动编辑 csproj 文件很快就会过时。

我想制作一个小型 Visual Studio AddIn,通过向卸载的项目上下文菜单添加额外的按钮来修复项目。

我面临的问题听起来很简单,但却困扰着我:我不知何故无法找到已卸载项目上下文菜单的 CommandBar。

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

非常感谢!

NuGet package restore does not play well with the PostSharp package, this is of great annoyance to me and manually editing the csproj files is quickly becoming old.

I want to make a small Visual Studio AddIn that fixes the project for me by adding an extra button to the unloaded project context menu.

The problem I am facing sounds simple but haunts me: I somehow can't locate the CommandBar for the unloaded project context menu.

Thus, summarizing the question: What is the CommandBar name for the unloaded project context menu.

Many thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一抹苦笑 2024-12-31 23:34:16

显然:“存根项目”。可以在以下位置的列表中找到: CommandBar 名称

总结一下注册此功能的 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.
            }
        }
    }

Apparantly: "Stub Project". Found in the list at: CommandBar names

To sum up the OnConnection method to register this:

    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.
            }
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文