VS2010插件,如何将命令添加到上下文菜单?

3

我知道已经有一些关于这个的帖子,但是对我来说都不起作用。

我的需求: 我需要在Visual Studio源代码控制资源管理器的上下文菜单中添加一个新条目。为此,我开始了一个新的Add-In项目。

我使用的内容: 我使用了这篇文章作为指南。 http://blogs.msdn.com/b/team_foundation/archive/2010/06/24/extending-work-item-tracking-context-menus.aspx

不起作用的部分: 我没有收到任何异常,菜单就是不会显示出来,无论我把它添加到哪里。

一些代码片段:

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
                }
}

我正在使用“团队项目”菜单名称进行测试。VSIPLogging告诉我,如果我在我们的TFS团队项目上右键单击,这就是菜单的名称。我也尝试了其他菜单,但没有成功。
以下是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);
    }

当我仔细查看命令栏“parent”时,它会给我带来很多异常:

如果我更仔细地观察它,“accChildCount = 'parent.accChildCount'”会引发类型为“Microsoft.VisualStudio.PlatformUI.Automation.DeprecatedException”的异常。

每个其他的“acc”值也是如此。

现在我真的不知道我做错了什么,或者还可以尝试什么来使其正常工作。我想要做的就是在源代码控制资源管理器中添加一个上下文菜单项,该项应调用Power Tools命令行exe来调用其中的“撤消未更改”的功能。


不用太担心 DeprecatedExceptions;这些可能只是因为微软不希望您使用过时的接口版本而存在。自首次发布以来,他们可能已经相当大地改变了接口,并且由于兼容性原因无法删除属性。 - takrl
你的QueryStatus方法是做什么的?该方法确定命令是隐藏还是可见,我注意到你指向的文章没有实现它的示例。 - Ann L.
1
我有几乎相同的代码,只是我将我的自定义命令添加到“IDTExtensibility2.OnStartupComplete”方法中。在“IDTExtensibility2.OnConnection”方法中,我使用“ext_ConnectMode.ext_cm_AfterStartup”而不是“ext_ConnectMode.ext_cm_UISetup”:if( connectMode == ext_ConnectMode.ext_cm_AfterStartup ) this.OnStartupComplete( ref custom ); - Garry English
1个回答

3
我很确定Visual Studio中的弹出窗口是CommandBarPopup类型。我还很确定您需要使命令/控件全局化,以便对它们保留引用,否则GC会将其删除。
您需要确保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);

需要注意以下几点:
  1. newCommand不是你代码中的局部变量,它被提升为全局变量以保持其存在(不过,这可能不是你遇到的问题,如果是这个问题-你会在第一次看到它然后它就消失了)。
  2. 你省略了参数,在这里ref ContextGUIDS是一个新对象数组,刚刚在方法调用之前声明,用于保存命令的GUID,它并不重要,只需添加它,重要的是接下来的参数,第一个告诉Visual Studio命令是否可见和启用:(int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,下一个给出命令应该长什么样子(我们是按钮)。
这只是一个起点,请参阅本文: 如何:使用Visual Studio命令栏弹出窗口从插件创建上下文菜单 祝你好运!

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