2010-10-05 129 views
3

我知道这里已经有一些线索,但我不会为我工作。VS2010加载项,添加命令到上下文菜单?

我想要的: 我需要在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); 
    } 

命令栏“父”让我颇有些例外,如果我仔细看看吧:

accChildCount =“parent.accChildCount”抛出'Microsoft.VisualStudio.PlatformUI.Automation.DeprecatedException'类型的例外情况对于每个其他“acc”值都是相同的。

现在我真的不知道我做错了什么,或者我可以尝试做什么。我想要做的就是在源代码控制浏览器中有一个上下文菜单条目,它应该调用电动工具命令行EXE来调用它的“撤消不变”功能。

+0

我不会担心DeprecatedExceptions太多;那些可能就在那里,因为MS不希望你使用过时的界面版本。自从它首次发布以来,它们可能会稍微改变界面,并且出于兼容性原因无法删除这些属性。 – takrl 2011-07-06 16:01:39

+0

你的QueryStatus方法在做什么?该方法确定命令是隐藏还是可见,并且我注意到您指向的文章没有给出实现它的示例。 – 2011-11-14 20:55:37

+1

我有几乎相同的代码,除了我在“IDTExtensibility2.OnStartupComplete”方法中添加自定义命令。 与 “IDTExtensibility2.OnConnection” 方法中,我使用的 “ext_ConnectMode.ext_cm_AfterStartup” 而不是 “ext_ConnectMode.ext_cm_UISetup”: 如果(connectMode == ext_ConnectMode.ext_cm_AfterStartup) this.OnStartupComplete(参照定制); – 2011-11-29 21:42:54

回答

3

我很确定Visual Studio中的Popups是CommnadBarPopup类型的。 另一件我确定的事情是,你需要让你的命令/控件是全局的,这样才能保留它们,否则GC会杀死它们。

你需要确保的是,在AddCommand命令名称不包含点,并在查询/ 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. 你忽略了参数,这里的参考ContextGUIDS是一个新的对象[],它在方法调用之前被声明来保存命令的guid,它并不重要,只是添加它,重要的是下一个参数(int)vsCommandStatus.vsCommandStatusSupported +(int)vsCommandStatus.vsCommandStatusEnabled下一步给出一些关于你的命令应该是什么样的提示(在我们的例子中是按钮)。

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

祝你好运!

相关问题