【问题标题】:Add submenu to context menu in Visual Studio Extension将子菜单添加到 Visual Studio 扩展中的上下文菜单
【发布时间】:2016-09-02 20:58:32
【问题描述】:

我想在 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 按预期显示为“顶级”命令。 其他命令和菜单根本不显示。

为什么菜单不显示,如何使它可见?

【问题讨论】:

标签: c# visual-studio-extensions


【解决方案1】:

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

【讨论】:

  • 你是对的,设置很好。有时您只需要重新启动即可。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多