描述
我开发了一个Visual Studio扩展(VSPackage),它将新的项目类型添加到Visual Studio(使用CPS Project System)。我还在VSPackage中添加了一些命令。
在解决方案资源管理器中右键单击我的项目节点时,我想要显示一个自定义的上下文菜单。
例
mycommand
).
我试过了..
Parent
of my custom command to IDM_VS_CTXT_PROJNODE
.
题
C#
项目,上下文菜单应该是默认项目,如果我添加一个MyProjectType
项目,我想在右键单击时看到不同的上下文菜单在解决方案资源管理器中的项目节点上。你和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中你可以检查项目的类型并返回一个布尔控件,如果额外的上下文菜单应该显示是或否