如何创建带有子级按钮的右键单击顶级菜单的解决方案?(visual studio 扩展名)

how to create a solution right click top-level menu with sub-level button?(visual studio extension)

Visual Studio 版本: 2019

我正在按照 adding a menu to the visual studio menu bar 创建菜单,有效,菜单将显示在 Extensions 菜单下。现在,我想将这些菜单移动到解决方案上下文菜单,所以我将 Menu>Parent>id 更改为 IDM_VS_CTXT_SOLNNODE。但它不起作用。

之前:

<?xml version="1.0" encoding="utf-8"?>
<CommandTable xmlns="http://schemas.microsoft.com/VisualStudio/2005-10-18/CommandTable" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <Extern href="stdidcmd.h"/>
  <Extern href="vsshlids.h"/>

  <Commands package="guidVSIXProject1Package">
    <Menus>
      <Menu guid="guidVSIXProject1PackageCmdSet" id="TopLevelMenu" priority="0x700" type="Menu">
        <Parent guid="guidSHLMainMenu" id="IDM_VS_CTXT_SOLNNODE" />
        <Strings>
          <ButtonText>TestMenu</ButtonText>
          <CommandName>TestMenu</CommandName>
        </Strings>
      </Menu>
    </Menus>

    <Groups>
      <Group guid="guidVSIXProject1PackageCmdSet" id="MyMenuGroup" priority="0x0600">
        <Parent guid="guidVSIXProject1PackageCmdSet" id="TopLevelMenu"/>
      </Group>
    </Groups>

    <Buttons>
      <Button guid="guidVSIXProject1PackageCmdSet" id="TestCommandId" priority="0x0100" type="Button">
        <Parent guid="guidVSIXProject1PackageCmdSet" id="MyMenuGroup" />
        <Icon guid="guidImages" id="bmpPic1" />
        <Strings>
          <ButtonText>Invoke TestCommand</ButtonText>
        </Strings>
      </Button>
    </Buttons>

    <Bitmaps>
      <Bitmap guid="guidImages" href="Resources\TestCommand.png" usedList="bmpPic1, bmpPic2, bmpPicSearch, bmpPicX, bmpPicArrows, bmpPicStrikethrough"/>
    </Bitmaps>
  </Commands>

  <Symbols>
    <GuidSymbol name="guidVSIXProject1Package" value="{fdde7b56-2c13-4a0b-bb96-d0b1c712c674}" />

    <GuidSymbol name="guidVSIXProject1PackageCmdSet" value="{23dc4a5e-5843-45cb-8b8b-c4a11184d73e}">
      <IDSymbol name="MyMenuGroup" value="0x1020" />
      <IDSymbol name="TestCommandId" value="0x0100" />
      <IDSymbol name="TopLevelMenu" value="0x1021"/>
    </GuidSymbol>

    <GuidSymbol name="guidImages" value="{f1008c6c-6b78-4876-93d1-b84e9a83c010}" >
      <IDSymbol name="bmpPic1" value="1" />
      <IDSymbol name="bmpPic2" value="2" />
      <IDSymbol name="bmpPicSearch" value="3" />
      <IDSymbol name="bmpPicX" value="4" />
      <IDSymbol name="bmpPicArrows" value="5" />
      <IDSymbol name="bmpPicStrikethrough" value="6" />
    </GuidSymbol>
  </Symbols>
</CommandTable>

之后:

<?xml version="1.0" encoding="utf-8"?>
<CommandTable xmlns="http://schemas.microsoft.com/VisualStudio/2005-10-18/CommandTable" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <Extern href="stdidcmd.h"/>
  <Extern href="vsshlids.h"/>

  <Commands package="guidVSIXProject1Package">
    <Menus>
      <Menu guid="guidVSIXProject1PackageCmdSet" id="TopLevelMenu" priority="0x700" type="Menu">
        <Parent guid="guidSHLMainMenu" id="IDG_VS_MM_TOOLSADDINS" /> <!--only change this id-->
        <Strings>
          <ButtonText>TestMenu</ButtonText>
          <CommandName>TestMenu</CommandName>
        </Strings>
      </Menu>
    </Menus>
    <!--same with before-->
</CommandTable>

您应该将 <Parent guid="guidSHLMainMenu" id="IDM_VS_CTXT_SOLNNODE" /> 设置为一组而不是一个菜单。

更改自:

<Groups>
  <Group guid="guidVSIXProject1PackageCmdSet" id="MyMenuGroup" priority="0x0600">
    <Parent guid="guidVSIXProject1PackageCmdSet" id="TopLevelMenu"/>
  </Group>
</Groups>

收件人:

<Groups>
  <Group guid="guidVSIXProject2PackageCmdSet" id="MyMenuGroup" priority="0x0600">
    <!--<Parent guid="guidVSIXProject2PackageCmdSet" id="TopLevelMenu"/>-->
    <Parent guid="guidSHLMainMenu" id="IDM_VS_CTXT_SOLNNODE"/>
  </Group>
</Groups>

这将将您的菜单从Extensions top menu移动到解决方案上下文(右键单击解决方案)。

但这也意味着您无法再从顶部菜单中找到该命令,如果您想让该命令在顶部菜单和解决方案上下文中都可用,请考虑使用 CommandPlacements