是否可以在Visual Studio加载项中添加多个命令?

时间:2011-03-02 23:42:47

标签: visual-studio visual-studio-addins

我希望能够使用许多命令创建Visual Studio加载项。在addin的OnConnection方法中,向导会生成此样板:

Command command = commands.AddNamedCommand2(_addInInstance, "MyAddin", "MyAddin", 
  "Executes the command for MyAddin", true, 59, ref contextGUIDS, ... )

这会在“工具”菜单上为MyAddin创建一个命令,但忽略我创建更多命令的任何尝试:

    Command command = commands.AddNamedCommand2(_addInInstance, "MyAddin command2", "MyAddin command2", 
  "Executes the command for MyAddin command 2", true, 59, ref contextGUIDS, ... )

这是Addins本身的限制吗,它们只能对应一个菜单项?还是必须以不同的方式完成?我应该写一个VSPackage吗?

1 个答案:

答案 0 :(得分:2)

我发现您实际上并不局限于在AddIn中创建单个命令。问题原来是命令名称不能像我在上面的例子中那样有空格。在我的插件的Connect方法中,我现在有一个循环遍历一个命令列表,将它们添加到应用程序的命令列表并为它们添加新的CommandBar

public class MyCommandDef {
  public String Name;
  public String MenuText;
  public String Binding;
}

...

 foreach (MyCommandDef command in CommandList.Commands) {
   try {
     Command newCmd = commands.AddNamedCommand(_addInInstance, command.Name, command.MenuText, "", true, 59, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled);
     if (command.Binding != null) {
       newCmd.Bindings = command.Binding;
     }
     CommandBarControl newCmdControl = (CommandBarControl)newCmd.AddControl(myCommandBar, myCommandBar.accChildCount + 1);
     if (lastCategory != command.Category) {
       // add a separator. how?
       if (newCmdControl!=null) {
         newCmdControl.BeginGroup=true;
       }
       lastCategory = command.Category;
     }
    } 
    catch (System.ArgumentException) {
      // command already exists, or has a space in it
    }
  }