在 Visual Studio 扩展程序的上下文菜单中添加子菜单

Add submenu to context menu in Visual Studio Extension

我想在 Visual Studio 的上下文菜单中添加一个子菜单。类似于 resharper 所做的:

我的设置如下: MyTopMenuGroup:包含Command1MyMenuController。 MenuController 本身又有另一个组,其中包含一些其他命令。不幸的是,MenuController 没有显示。

我的XAML:

<Groups>
  <Group guid="mypkg" id="MyTopMenuGroup" >
    <Parent guid="guidSHLMainMenu" id="IDM_VS_CTXT_ITEMNODE" />
  </Group>
  <Group guid="mypkg" id="MySubMenuGroup">
    <Parent guid="mypkg" id="MyMenuController" />
  </Group>
</Groups>

<Menus>
  <Menu guid="mypkg" id="MyMenuController" type="MenuController">
    <Parent guid="mypkg" id="MyTopMenuGroup" />
  </Menu>
</Menus>

<Buttons>
  <Button guid="mypkg" id="Command1" type="Button">
    <Parent guid="mypkg" id="MyTopMenuGroup" />
  </Button>
  <Button guid="mypkg" id="Command2" type="Button">
    <Parent guid="mypkg" id="MyMenuController" />
  </Button>
  <Button guid="mypkg" id="Command3" type="Button">
    <Parent guid="mypkg" id="MySubMenuGroup" />
  </Button>
  <Button guid="mypkg" id="Command4" type="Button">
    <Parent guid="mypkg" id="MySubMenuGroup" />
  </Button>
</Buttons>

将按钮添加到菜单的 C#:

OleMenuCommandService commandService = this.ServiceProvider.GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
if (commandService != null)
{
    var menuCommandID = new CommandID(CommandSet, Command1);
    var menuItem = new MenuCommand(this.MenuItemCallback, menuCommandID);
    commandService.AddCommand(menuItem);

    //etc, do this for all 4 Commands
    //no code to construct groups & menus (is this necessary?)
}

Command1 按预期显示为 "top-level" 命令。 其他命令和菜单根本不显示。

为什么菜单没有显示,我怎样才能让它可见?

您的 XAML 看起来不错(我假设按钮和菜单实际上有字符串部分)并且 Command3/Command4 应该可见。只需确保它们像 Command1 一​​样附加了 MenuItemCallbacks。