2011-03-02 46 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 
    } 
    } 
相關問題