7
在我的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
什麼都不做。我應該如何隱藏這個不支持菜單項的菜單項?
見UI上下文GUID的可能性在這裏:https://msdn.microsoft.com/en-us/library/bb166496.aspx 請注意,我們可以看到上面的GUID這個網頁上,這意味着UICONTEXT_SolutionExists。 – CSA