在AddIn中将一个项目添加到Visual Studio文件夹右键菜单

17

我想在Visual Studio 2012解决方案资源管理器中的右键菜单=>添加菜单中添加一个菜单项。当点击自定义项时,我可以使用我的模板添加项目。 我开发了一个Visual Studio插件来实现它,但遇到了一些问题。我能够将菜单项添加到右键菜单中,但无法满足我的要求。

  1. 菜单项应该是“添加”子菜单,而不是根菜单项。

  2. 我还需要菜单项仅在右键单击名为“Areas”的文件夹时显示。我不希望它在右键单击其他文件夹时显示。

这是我的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;

            //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:
            var bars=((Microsoft.VisualStudio.CommandBars.CommandBars)_applicationObject.CommandBars);

            Microsoft.VisualStudio.CommandBars.CommandBar menuBarCommandBar = bars["MenuBar"];

            //Find the Tools command bar on the MenuBar command bar:
            //CommandBarControl toolsControl = menuBarCommandBar.Controls[toolsMenuName];
            //CommandBarPopup toolsPopup = (CommandBarPopup)toolsControl;
            // get popUp command bars where commands will be registered.
            CommandBars cmdBars = (CommandBars)(_applicationObject.CommandBars);
            //CommandBar vsBarItem = cmdBars["Item"]; //the pop up for clicking a project Item
            CommandBar vsBarFolder = cmdBars["Web Project Folder"];
            CommandBar vsBarWebFolder = cmdBars["Web Folder"];

            //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, "ModuleAddin", "Add a Project", "Executes the command for ModuleAddin", true, 59, 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)
                {
                    //command.AddControl(toolsPopup.CommandBar, 1);
                    command.AddControl(vsBarFolder);
                    //CommandBarButton button = (CommandBarButton)command.AddControl(vsBarFolder, 3);
                    //button.BeginGroup = true;
                }
            }
            catch (System.ArgumentException argEx)
            {
                System.Diagnostics.Debug.Write("Exception in HintPaths:" + argEx.ToString());
            }
        }
    }

对于您的第二个请求,您可能想看一下这篇文章。http://www.diaryofaninja.com/blog/2014/02/18/who-said-building-visual-studio-extensions-was-hard - Kraven
1个回答

42

你不需要为此安装任何插件。

链接:http://nickmeldrum.com/blog/how-to-run-powershell-scripts-from-solution-explorer-in-visual-studio-2010

复制并粘贴博客文章...

步骤1:将“运行 PowerShell 脚本”添加为外部工具

  1. 在 Visual Studio 中,转到菜单:Tools | External Tools
  2. 点击“添加”按钮
  3. 添加以下表单值:

    • 标题:“在输出窗口中运行 PowerShell 脚本”
    • 命令:“C:\windows\system32\windowspowershell\v1.0\powershell.exe”
    • 参数:“-file "$(ItemPath)"
    • 初始目录:“$(ItemDir)”
    • 勾选“使用输出窗口”
    • (关闭退出现在会自动打开)
  4. 点击“应用”按钮

  5. 点击“添加”按钮

  6. 添加以下表单值:

    • 标题:“在 Visual Studio 外部运行 PowerShell 脚本”
    • 命令:“C:\windows\system32\windowspowershell\v1.0\powershell.exe”
    • 参数:“-file "$(ItemPath)"
    • 初始目录:“$(ItemDir)”
    • 不要勾选“使用输出窗口”
    • 勾选“关闭退出”
  7. 点击“确定”按钮

它们应该长成这样: >

步骤2:奇怪的一步,相信我!

检查它在外部工具列表中的索引位置。默认情况下,我的位置是6和7。(我认为默认情况下创建 GUID 是第一位!)

步骤3:将其与上下文菜单连接

  1. 转到菜单:Tools | Customize | Commands
  2. 选择“Context Menu”单选按钮
  3. 向下滚动到“Project and Solution Context Menus | Item”(长得像噩梦一样的菜单,输入“Proj”以大致到达正确的位置)
  4. 点击“添加命令”按钮
  5. 选择类别:“工具”和命令:“External Command 7”(或者您从“奇怪的第2步”中获得的任何其他位置)
  6. 点击“确定”按钮
  7. 然后设置第二个命令:
  8. 选择类别:“工具”和命令:“External Command 8”(或者您为另一个命令获得的其他位置)
  9. 再次点击“确定”按钮
  10. 将它们移动到您满意的顺序(我通常将它们放在“打开方式…”下面的某个地方)>

步骤4:添加键盘快捷方式

  1. 转到菜单:Tools | Options
  2. 选择“环境|键盘”部分
  3. 在列表中找到 Tools.ExternalCommandN 项(又是长得像噩梦一样的列表,再次输入“Tools”将大致到达目的地)
  4. 为每个命令选择您喜欢的快捷键:我喜欢分别使用 CTRL SHIFT PCTRL SHIFT ALT P

ohai


2
这太棒了。 - Maciej Szpakowski
2
太棒了,定制化非常成功!现在我只需要找出如何自动化这个过程,为此编写一个设置程序,我就会100%满意了。 - C4d
@C4u 你有没有想到一种编写此设置脚本的方法? - crush
@crush自动化的唯一方法是解析位于“AppData \ Local”(在Windows中)路径下的“CurrentSettings.vssetings”文件。 - z3nth10n

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接