为什么我的扩展程序的菜单项没有出现?

Why does my extension's menu item not appear?

我正在构建我的第一个 VS 扩展,以允许用户 encrypt/decrypt web.configmailSettings/smtp 部分。

我想在主 VS 工具菜单中添加一个包含 2 个子项的菜单项:

Config Encryptor
  Encrypt Mail Settings
  Decrypt Mail Settings

.vsct文件的相关(我希望)部分如下:

<Menus>
  <Menu guid="guidEncryptConfigCommandPackageCmdSet" id="ConfigEncryptorMenu" priority="0x0100" type="Menu">
    <Parent guid="guidEncryptConfigCommandPackageCmdSet" id="ConfigEncryptorMenuGroup" />
    <Strings>
      <MenuText>Config Encryptor</MenuText>
      <ButtonText>Config Encryptor</ButtonText>
      <CommandName>Config Encryptor</CommandName>
    </Strings>
  </Menu>
</Menus>

<Groups>      
  <Group guid="guidEncryptConfigCommandPackageCmdSet" id="ConfigEncryptorMenu" priority="0x0200">
    <Parent guid="guidSHLMainMenu" id="IDM_VS_MENU_TOOLS" />
  </Group>
  <Group guid="guidEncryptConfigCommandPackageCmdSet" id="ConfigEncryptorMenuGroup" priority="0x0100">
    <Parent guid="guidEncryptConfigCommandPackageCmdSet" id="ConfigEncryptorMenu" />
  </Group>
</Groups>

<Buttons>
  <Button guid="guidEncryptConfigCommandPackageCmdSet" id="cmdidEncryptConfigCommand" priority="0x0100" type="Button">
    <Parent guid="guidEncryptConfigCommandPackageCmdSet" id="ConfigEncryptorMenuGroup" />
    <Strings>
      <ButtonText>Encrypt Mail Settings</ButtonText>
    </Strings>
  </Button>
  <Button guid="guidEncryptConfigCommandPackageCmdSet" id="cmdidDecryptConfigCommand" priority="0x0100" type="Button">
    <Parent guid="guidEncryptConfigCommandPackageCmdSet" id="ConfigEncryptorMenuGroup" />
    <Strings>
      <ButtonText>Decrypt Mail Settings</ButtonText>
    </Strings>
  </Button>
</Buttons>

<GuidSymbol name="guidEncryptConfigCommandPackageCmdSet" value="{2c763b06-e83f-4c03-8fc6-3a00416b361e}">
  <IDSymbol name="ConfigEncryptorMenu" value="0x1010" />
  <IDSymbol name="ConfigEncryptorMenuGroup" value="0x1020" />
  <IDSymbol name="cmdidEncryptConfigCommand" value="0x0100" />
  <IDSymbol name="cmdidDecryptConfigCommand" value="0x1021"  />
</GuidSymbol>

我在VS的新实例中调试扩展项目时没有出现菜单项,我做错了什么?

有可能是因为你定义了一个ID为ConfigEncryptorMenumenu,同时又定义了一个ID为ConfigEncryptorMenugroup,搞砸了结构。

让我们定义一个名为 GroupForSubMenu 的新 IDSymbol:

<GuidSymbol name="guidEncryptConfigCommandPackageCmdSet" value="{2c763b06-e83f-4c03-8fc6-3a00416b361e}">
 ......
  <!-- New IDSymbol -->
  <IDSymbol name="GroupForSubMenu" value="0x1050"  />
</GuidSymbol>

然后把first Group的内容改成:

  <Group guid="guidEncryptConfigCommandPackageCmdSet" id="GroupForSubMenu" priority="0x0200">
    <Parent guid="guidSHLMainMenu" id="IDM_VS_MENU_TOOLS" />
  </Group>

并更改 Menu 部分中 <Parent> 的值:

<Parent guid="guidEncryptConfigCommandPackageCmdSet" id="ConfigEncryptorMenuGroup" /> 

<Parent guid="guidEncryptConfigCommandPackageCmdSet" id="GroupForSubMenu" />

MenuText 不是必需的,原来的父关系似乎是 ConfigEncryptorMenuGroup's parent is ConfigEncryptorMenuConfigEncryptorMenu's parent is ConfigEncryptorMenuGroup。更正组和菜单之间的关系,问题可以解决。

我在尝试将旧扩展移植到 VS2022 时遇到了非常相似的问题。最初我移植的解决方案中不会出现任何工具栏(这可能是由于此处提到的原因Visual Studio VSIX Extension not showing in Tools menu),所以我放弃并使用“VSIX Project w/Command(Community)”模板创建了一个全新的扩展在“Extensibility Essentials 2022”扩展中。

无论如何,在我的旧 VSCT 文件中添加 'Menus' 和 'Groups' 后,调试扩展时工具栏没有出现。

我的案例的答案在 https://docs.microsoft.com/en-us/visualstudio/extensibility/adding-a-toolbar?view=vs-2022 的第 4 点底部找到 引用“默认情况下,如果工具栏没有命令,它不会出现。”

一旦我调整了默认按钮的父级,我的工具栏就会出现在 VS2022 中。