在我的VS扩展程序中,我需要为新项目类型添加菜单项。但我希望它只显示我的自定义类型。所以我将此代码添加到.vcst文件:
<Button guid="_Interactive_WindowCmdSet" id="cmdidLoadUI" priority="0x0100" type="Button">
<Parent guid="_Interactive_WindowCmdSet" id="ProjectItemMenuGroup" />
<Icon guid="guidImages" id="bmpPic1" />
<CommandFlag>DynamicVisibility</CommandFlag>
<Strings>
<ButtonText>Load</ButtonText>
</Strings>
</Button>
<Group guid="_Interactive_WindowCmdSet" id="ProjectItemMenuGroup" priority="0x0600">
<Parent guid="guidSHLMainMenu" id="IDM_VS_CTXT_PROJNODE"/>
</Group>
并将此代码添加到包初始化:
// Create the command for the menu item.
CommandID projectMenuCommandID = new CommandID(GuidList.Interactive_WindowCmdSet, (int)PkgCmdIDList.cmdidLoadUI);
OleMenuCommand projectmenuItem = new OleMenuCommand(LoadUIMenuItemCallback, projectMenuCommandID);
projectmenuItem.BeforeQueryStatus += projectmenuItem_BeforeQueryStatus;
mcs.AddCommand(projectmenuItem);
查询状态处理程序是:
private void projectmenuItem_BeforeQueryStatus(object sender, EventArgs e)
{
OleMenuCommand menuCommand = sender as OleMenuCommand;
if (menuCommand != null)
menuCommand.Visible = IsProjectOfRightType(GetSelected<Project>());
}
问题是 - 这个状态处理程序永远不会被调用。所以我为所有项目类型显示了这个菜单项。
我也尝试在我的包上实现IOleCommandTarget
接口,例如:
public int QueryStatus(ref Guid guidCmdGroup, uint cCmds, OLECMD[] prgCmds, IntPtr pCmdText)
{
// Disable all commands in case if project is VisuaART project, otherwise - disable them.
OLECMDF cmdf;
for (int i = 0; i < cCmds; i++)
{
var command = prgCmds[i];
if (command.cmdID == PkgCmdIDList.cmdidLoadUI)
{
if (IsProjectOfRightType(GetSelected<Project>()))
command.cmdf = (uint)COMMAND_SUPPORTED;
else
command.cmdf = (uint)COMMAND_UNSUPPORTED;
}
}
return VSConstants.S_OK;
}
private const OLECMDF COMMAND_SUPPORTED = OLECMDF.OLECMDF_SUPPORTED | OLECMDF.OLECMDF_ENABLED;
private const OLECMDF COMMAND_UNSUPPORTED = OLECMDF.OLECMDF_INVISIBLE;
但这也没有帮助。调用方法,但设置OLECMDF.OLECMDF_INVISIBLE
不执行任何操作。如何为不支持的菜单项隐藏此菜单项?
答案 0 :(得分:7)
问题可能与包的负载有关。要自动加载Package,只需将此属性添加到Package Class:
[ProvideAutoLoad("f1536ef8-92ec-443c-9ed7-fdadf150da82")]
例:
.
.
[ProvideAutoLoad("f1536ef8-92ec-443c-9ed7-fdadf150da82")]
public sealed class MyPackageTest : Package
{
.
.
如果您不添加此属性,则只需点击套餐中的任何按钮即可加载您的课程。
我希望我有所帮助。