2011-03-02 48 views
1

我希望能够用许多命令创建一个Visual Studio加载项。在插件的OnConnection方法向导生成这个样板:是否可以在Visual Studio加载项中添加多个命令?

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吗?

+0

您发布锅炉板代码工作而不是你无法工作的代码。不是一个好主意。 – 2011-03-03 01:13:12

+0

好吧,我已经这样做了,但是我不认为它在这个例子中是特别有用的,因为它只是一个用不同命令名复制上一行的例子 – 2011-03-03 09:33:42

回答

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 
    } 
    } 
相关问题