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

时间:2010-10-05 08:20:51

标签: c# add-in contextmenu

我知道已经有一些线索,但我不会为我工作。

我想要的: 我需要Visual Studio源代码管理资源管理器的上下文菜单中的新条目。为此,我开始了一个新的插件项目。

我用过的东西: 我用这篇文章作为指导。 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”值都相同。

现在我真的不知道我做错了什么或者我还能尝试做些什么。我想要做的就是在源代码管理资源管理器中有一个上下文菜单条目,它应该调用电动工具命令行exe来调用它的“撤消未更改”功能。

1 个答案:

答案 0 :(得分:3)

我很确定Visual Studio中的Popups是CommnadBarPopup类型。 我非常确定的另一件事是你需要使你的命令/控件全局,所以引用它们,否则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. 你是ommited参数,这里的ref ContextGUIDS是一个新的对象[],它在方法调用之前声明为命令保存guid,它并不重要,只需添加它,重要的是下一个参数,第一个告诉visual studio命令是否可见并启用:(int) vsCommandStatus.vsCommandStatusSupported +(int) vsCommandStatus.vsCommandStatusEnabled ,然后下一个提示你的命令应该是这样的(在我们的例子中是按钮)。
  3. 这只是一个起点,plaese参考这篇文章: HOWTO: Create a context menu using a Visual Studio commandbar popup from an add-in

    祝你好运!

相关问题