如何在解决方案资源管理器中为特定项目类型自定义上下文菜单?

问题描述 投票:3回答:1

描述


我开发了一个Visual Studio扩展(VSPackage),它将新的项目类型添加到Visual Studio(使用CPS Project System)。我还在VSPackage中添加了一些命令。

在解决方案资源管理器中右键单击我的项目节点时,我想要显示一个自定义的上下文菜单。


For example: in the screen shot below, I want to get rid of the Build command and add a custom command (e.x. mycommand).

enter image description here

我试过了..


Setting the Parent of my custom command to IDM_VS_CTXT_PROJNODE.


  • 当我创建一个新的自定义项目类型时,如何在解决方案资源管理器中为我的项目节点创建一个新的上下文菜单?
  • 如何仅为自定义项目删除/添加命令到上下文菜单:如果我有一个C#项目,上下文菜单应该是默认项目,如果我添加一个MyProjectType项目,我想在右键单击时看到不同的上下文菜单在解决方案资源管理器中的项目节点上。
vsix vspackage
1个回答
1
投票

你和IDM_VS_CTXT_PROJNODE父母关系密切。

以下是我在FluentMigratorRunner扩展中实现它的方法,该扩展仅显示项目的上下文菜单项,如果它引用了FluentMigrator NuGet包。

步骤1:在上下文菜单中添加子菜单

<Menus>
  <Menu guid="guidCmdSet" id="packageMenu" priority="0x0300" type="Menu">
    <Parent guid="guidSHLMainMenu" id="IDG_VS_CTXT_PROJECT_BUILD" />
    <CommandFlag>DynamicVisibility</CommandFlag>
    <CommandFlag>DefaultInvisible</CommandFlag>
    <Strings>
      <ButtonText>CPSProject</ButtonText>
      <CommandName>CPSProject</CommandName>
    </Strings>
  </Menu>

请注意添加的特殊CommandFlag元素。

第2步:在菜单中添加一个组

<Groups>  
  <Group guid="guidCmdSet" id="packageMenuGroup" priority="0x0600">
    <Parent guid="guidCmdSet" id="packageMenu" />
  </Group>    
</Groups>

第3步:添加一个按钮

  <Button guid="guidCmdSet" id="specialBuildActionId" priority="0x0100" type="Button">
    <Parent guid="guidCmdSet" id="packageMenuGroup" />
    <CommandFlag>DynamicVisibility</CommandFlag>
    <Strings>
      <ButtonText>Special build</ButtonText>
    </Strings>

第4步:在* Package.cs中添加菜单

protected override async System.Threading.Tasks.Task InitializeAsync(System.Threading.CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
{
    // Initialize the Fluent Migrator Menu, should only be visible for projects with FluentMigrator reference
    var mcs = await GetServiceAsync(typeof(IMenuCommandService)) as OleMenuCommandService;
    var menuCommandId = new CommandID(packageCmdSetGuidString, 0x1010);
    var menuItem = new OleMenuCommand(null, menuCommandId);
    menuItem.BeforeQueryStatus += MenuItem_BeforeQueryStatus;

    mcs.AddCommand(menuItem);
}

private void MenuItem_BeforeQueryStatus(object sender, EventArgs e) =>
    ((OleMenuCommand)sender).Visible = ???;

请注意添加的BeforeQueryStatus事件处理程序。

在那个eventhandler中你可以检查项目的类型并返回一个布尔控件,如果额外的上下文菜单应该显示是或否

© www.soinside.com 2019 - 2024. All rights reserved.