简体   繁体   English

VS2010加载项,向上下文菜单添加命令?

[英]VS2010 Add-In, adding a command to a context menu?

I know there are already some threads about this, but I just won't work for me. 我知道已经有一些线索,但我不会为我工作。

What I want: I need a new entry in a context menu of the Visual Studio Source Control Explorer. 我想要的:我需要Visual Studio源代码管理资源管理器的上下文菜单中的新条目。 For this I started a new Add-In Project. 为此,我开始了一个新的插件项目。

What I used: I used this article as a guide. 我用过的:我用这篇文章作为指导。 http://blogs.msdn.com/b/team_foundation/archive/2010/06/24/extending-work-item-tracking-context-menus.aspx http://blogs.msdn.com/b/team_foundation/archive/2010/06/24/extending-work-item-tracking-context-menus.aspx

What is not working: I don't get any exceptions, the menu just won't show up, no matter where I add it. 什么是无效的:我没有任何例外,菜单就不会出现,无论我在哪里添加它。

Some code snippets: 一些代码片段:

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)
{
   AddCommandToContextMenu(
                         "Team Project", // context menu Name
                         "ClearQuery", // menu reference name
                         "Clear", // display name
                         47, // command icon
                         1);    // command placement, 1= first item on top
                }
}

I am using "Team Project" menu name for testing. 我正在使用“团队项目”菜单名称进行测试。 VSIPLogging tells me, that this is the name of the menu if I make a right click on our TFS Team Project. VSIPLogging告诉我,如果我右键单击我们的TFS团队项目,这就是菜单的名称。 I also tried other menus without success. 我也尝试了其他菜单但没有成功。

Here are the AddCommandToContextMenu functions: 以下是AddCommandToContextMenu函数:

private void AddCommandToContextMenu(string menuName, string commandName, string commandText, int iconId, int position)
    {

                    CommandBar contextMenu = ((CommandBars)_applicationObject.CommandBars)[menuName];

                    AddCommand(contextMenu, commandName, commandText, iconId, position);
    }



private void AddCommand(CommandBar parent, string commandName, string commandText, int iconId, int position)

    {
                     Commands2 commands = (Commands2)_applicationObject.Commands;
                    //create the command
                    Command newCommand = commands.AddNamedCommand2(_addInInstance, commandName, commandText, commandText, true, iconId);
                    // add it to parent menu
                    newCommand.AddControl(parent, position);
    }

The commandbar "parent" gives me quite some exceptions, if I take a closer look at it: 命令栏“parent”给了我一些例外,如果我仔细看看它:

accChildCount = 'parent.accChildCount' threw an exception of type 'Microsoft.VisualStudio.PlatformUI.Automation.DeprecatedException' accChildCount ='parent.accChildCount'引发类型'Microsoft.VisualStudio.PlatformUI.Automation.DeprecatedException'的异常

The same for every other "acc" value. 每个其他“acc”值都是一样的。

Now I really don't know what I did wrong or what else I could try to make this work. 现在我真的不知道我做错了什么或者我还能尝试做些什么。 All I want to do is to have a context menu entry in the source control explorer, which should call the power tools command line exe to call the "undo unchanged" function of it. 我想要做的就是在源代码管理资源管理器中有一个上下文菜单条目,它应该调用电动工具命令行exe来调用它的“撤消未更改”功能。

I am pretty sure the Popups in Visual Studio were of CommnadBarPopup type. 我很确定Visual Studio中的Popups是CommnadBarPopup类型。 The other thing I am pretty sure was that you need to make your commands / controls global so a reference is kept on them, otherwise GC will kill them. 我非常确定的另一件事是你需要使你的命令/控件全局,所以引用它们,否则GC会杀死它们。

You need to make sure that the command name in the AddCommand doesn't contain dots, and in the Query / Exec functions it does, eg: 您需要确保AddCommand中的命令名称不包含点,并且在Query / Exec函数中它确实如此:例如:

newCommand = commands.AddNamedCommand2(_addInInstance, commandName, commandText, commandText, true, iconId, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,(int)vsCommandStyle.vsCommandStylePictAndText,vsCommandControlType.vsCommandControlTypeButton);

Few things to note here: 这里有几点需要注意:

  1. newCommand is not a local variable as in your code, it is promoted to a global variable to keep it alive (anyway, this is not your case probably, if this was the problem - you would see it the first time and then it will disappear). newCommand不是你的代码中的局部变量,它被提升为一个全局变量来保持它的活着(无论如何,这可能不是你的情况,如果这是问题 - 你会第一次看到它然后它会消失)。
  2. You ommited parameters, the ref ContextGUIDS here is a new object[] that was declared just before the method call to hold the guid for the command, it is not that important, just add it, what is important are the next parameters, the first one tells visual studio if the command is visible and enabled : (int) vsCommandStatus.vsCommandStatusSupported + (int) vsCommandStatus.vsCommandStatusEnabled and the next give some hint on what your command should look like (button in our case). 你是ommited参数,这里的ref ContextGUIDS是一个新的对象[],它是在方法调用之前声明的,用于保存命令的guid,它并不重要,只需添加它,重要的是下一个参数,第一个一个告诉visual studio命令是否可见并启用:(int) vsCommandStatus.vsCommandStatusSupported +(int) vsCommandStatus.vsCommandStatusEnabled ,下一个提示你的命令应该是什么样的(在我们的例子中是按钮)。

This is just a start point, plaese refer to this article: HOWTO: Create a context menu using a Visual Studio commandbar popup from an add-in 这只是一个起点,plaese参考这篇文章: HOWTO:使用加载项中的Visual Studio命令栏弹出窗口创建上下文菜单

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM