【问题标题】:VS2010 Add-In, adding a command to a context menu?VS2010 Add-In,在上下文菜单中添加命令?
【发布时间】:2011-12-15 23:11:47
【问题描述】:

我知道已经有一些关于此的主题,但我不会为我工作。

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

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

什么不工作: 我没有遇到任何异常,无论我在哪里添加菜单,它都不会出现。

一些代码sn-ps:

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

命令栏“父级”给了我一些例外,如果我仔细看看的话:

accChildCount = 'parent.accChildCount' 引发了“Microsoft.VisualStudio.PlatformUI.Automation.DeprecatedException”类型的异常

每个其他“acc”值都相同。

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

【问题讨论】:

  • 我不会太担心 DeprecatedExceptions;这些可能就在那里,因为 MS 不希望您使用过时版本的界面。自首次发布以来,他们可能对界面进行了相当大的更改,并且出于兼容性原因无法删除属性。
  • 您的 QueryStatus 方法在做什么?该方法确定命令是隐藏还是可见,我注意到您指向的文章没有给出实现它的示例。
  • 我有几乎相同的代码,除了我在“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 );

标签: c# add-in contextmenu


【解决方案1】:

我很确定 Visual Studio 中的弹出窗口属于 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. 你省略了参数,这里的 ref ContextGUIDS 是一个新的对象[],它是在方法调用之前声明的,用于保存命令的 guid,它并不重要,只需添加它,重要的是下一个参数,第一个告诉 Visual Studio 命令是否可见并启用: (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled 下一个给出一些关于什么的提示您的命令应该看起来像(在我们的例子中是按钮)。

这只是一个起点,请参考这篇文章: HOWTO: Create a context menu using a Visual Studio commandbar popup from an add-in

祝你好运!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多