代码编辑器中的 .cshtml 文件不显示上下文菜单条目

Context menu entry not appearing for .cshtml files in code editor

我创建了 a Visual Studio extension,它在右键单击两个位置时出现的上下文菜单中添加了两个条目:一个是解决方案资源管理器中的项目,另一个是打开的代码编辑器中的任意位置 window。

我面临的问题是在代码编辑器中单击时菜单项没有出现window如果正在编辑的文件是.cshtml 文件(虽然在解决方案资源管理器中单击时确实会出现)。对于我测试过的任何其他文件类型,它都可以正常工作。

下面是我在 .vsct 文件中定义菜单条目的方式:

<Groups>
  <Group guid="guidBrowseInRemoteGitRepoCommandPackageCmdSet" 
         id="MyMenuGroup" priority="0x0600">
    <Parent guid="guidSHLMainMenu" id="IDM_VS_CTXT_ITEMNODE"/>
  </Group>
  <Group guid="guidBrowseInRemoteGitRepoCommandPackageCmdSet" 
         id="MyMenuGroup" priority="0x0600">
    <Parent guid="guidSHLMainMenu" id="IDM_VS_CTXT_CODEWIN"/>
  </Group>
</Groups>

<Buttons>
    <Button guid="guidBrowseInRemoteGitRepoCommandPackageCmdSet" 
            id="BrowseInRemoteGitRepoCommandId" priority="0x0100" type="Button">
    <Parent guid="guidBrowseInRemoteGitRepoCommandPackageCmdSet" id="MyMenuGroup" />
    <Icon guid="guidImages" id="bmpPicGit" />
    <CommandFlag>DynamicVisibility</CommandFlag>
    <Strings>
      <ButtonText>Browse in remote repository</ButtonText>
    </Strings>
  </Button>

  <Button guid="guidBrowseInRemoteGitRepoCommandPackageCmdSet"
          id="CopyRemoteGitRepoUrlCommandId" priority="0x0100" type="Button">
  <Parent guid="guidBrowseInRemoteGitRepoCommandPackageCmdSet" id="MyMenuGroup" />
  <Icon guid="guidImages" id="bmpPicGit" />
  <CommandFlag>DynamicVisibility</CommandFlag>
  <Strings>
    <ButtonText>Copy URL of remote repository version</ButtonText>
  </Strings>
  </Button>
</Buttons>

菜单条目在命令构造函数中创建为 OleMenuCommand 的实例:

var commandService = this.ServiceProvider.GetService(typeof(IMenuCommandService)) 
    as OleMenuCommandService;
if (commandService == null) return;

var browseMenuCommandID = new CommandID(CommandSet, BrowseCommandId);
var browseMenuItem = new OleMenuCommand(BrowseMenuItemCallback, browseMenuCommandID);
browseMenuItem.BeforeQueryStatus += MenuItemOnBeforeQueryStatus;
commandService.AddCommand(browseMenuItem);
//same for the other entry

MenuItemOnBeforeQueryStatus 根据需要设置 senderVisibleEnabled 为真或假。

那么,我在这里缺少什么?

编辑:

为了完整起见,以下是我需要在 .vsct 文件中进行的更改以实现

1) 在<Symbols>中添加了以下内容:

<GuidSymbol name="guidCshtmlCodeEditor" value="{78F03954-2FB8-4087-8CE7-59D71710B3BB}" />

2) 在<Groups>中添加了以下内容:

<Group guid="guidBrowseInRemoteGitRepoCommandPackageCmdSet" id="MyMenuGroup" priority="0x0600">
    <Parent guid="guidCshtmlCodeEditor" id="IDM_VS_TOOL_STANDARD"/>
</Group>

埃德的回答。多尔 (https://social.msdn.microsoft.com/Forums/vstudio/en-US/7c5eb211-3985-426c-a3a2-9da4473dbaf4/my-context-menu-item-made-by-menu-command-in-visual-studio-package-is-not-shown-in-cshtml-files?forum=vsx):

.cshtml 文件显示不同的上下文菜单。

如果您使用 EnableVSIPLogging 注册表值,(HKEY_CURRENT_USER\SOFTWARE\Microsoft\VisualStudio.0\General),如这篇旧文章所述:

http://blogs.msdn.com/b/dr._ex/archive/2007/04/17/using-enablevsiplogging-to-identify-menus-and-commands-with-vs-2005-sp1.aspx

您会看到正在使用两个不同的上下文菜单。

.CS 文件使用:

Guid = {D309F791-903F-11D0-9EFC-00A0C911004F}
GuidID = 4
CmdID = 1037
Type = 0x00000400
Flags = 0x00000000
NameLoc = Code Window

和 .CSHTML 文件使用:

Guid = {78F03954-2FB8-4087-8CE7-59D71710B3BB}
GuidID = 395
CmdID = 1
Type = 0x00000400
Flags = 0x00000000
NameLoc = HTML Context

因此您需要相应地修改您的 .vsct 文件。