Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic Visibility of menu item

In my VS extension I need to add menu item for my new project type. But I want it to show for my custom type only. So I added this code to .vcst file:

  <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>

And added this code to package initialization:

            // 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);

And query status handler is:

    private void projectmenuItem_BeforeQueryStatus(object sender, EventArgs e)
    {
        OleMenuCommand menuCommand = sender as OleMenuCommand;

        if (menuCommand != null)
            menuCommand.Visible = IsProjectOfRightType(GetSelected<Project>());
    }

The problem is - this status handler never get called. So I have this menu item showed for all project types.

I've tried also to implement IOleCommandTarget interface on my package, like:

    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;

But that doesn't helped either. Method is called, but setting OLECMDF.OLECMDF_INVISIBLE does nothing. What should I do to hide this menu item for unsupported menu items?

like image 369
Seekeer Avatar asked Feb 21 '14 08:02

Seekeer


1 Answers

Probably the problem is related with the load of the Package. To load the Package automatically just add this attribute to your Package Class:

[ProvideAutoLoad("f1536ef8-92ec-443c-9ed7-fdadf150da82")]

Ex.:

.
.
[ProvideAutoLoad("f1536ef8-92ec-443c-9ed7-fdadf150da82")]
public sealed class MyPackageTest : Package
{
.
.

When you don’t add this attribute your class will be loaded just when you click on any button of your Package.

I hope I have helped.

like image 89
CSA Avatar answered Sep 25 '22 13:09

CSA