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

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

我正在使用“Team Project”菜单名称进行测试。 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”给了我很多异常(exception),如果我仔细看一下:

accChildCount = 'parent.accChildCount' 抛出类型为 'Microsoft.VisualStudio.PlatformUI.Automation.DeprecatedException' 的异常

其他所有“acc”值都相同。

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

最佳答案

我很确定 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

祝你好运!

关于c# - VS2010 加载项,向上下文菜单添加命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3861946/

相关文章:

c# - 根据层次结构在列表中搜索字符串

visual-studio - Visual Studio加载项应在哪里存储其设置?

visual-studio-2010 - 如何在 Visual Studio 2010 中管理加载项?

wpf - WPF MVVM : Find out on which header context menu has been clicked

c# - 如何对 ToolStripItem 集合中的项目进行排序?

c# - 确定两个 SyntaxToken 是否相同

下拉菜单的 C# Pager 问题

c# - DataGridViewCell.FormattedValue 是如何更新的?

c++ - 如何在 C++ 中捕获错误并调试在 Visual Studio 2010 下创建的 Excel DLL 加载项?

c# - 如何将 ContextMenuStrip 添加到 ToolStripMenuItem