【问题标题】:Dynamic Visibility of menu item菜单项的动态可见性
【发布时间】:2014-02-21 08:43:36
【问题描述】:

在我的 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 什么也不做。对于不受支持的菜单项,我应该怎么做才能隐藏此菜单项?

【问题讨论】:

    标签: c# visual-studio-2010 visual-studio visual-studio-2012 vsix


    【解决方案1】:

    问题可能与Package的负载有关。要自动加载包,只需将此属性添加到您的包类:

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

    例如:

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

    如果您不添加此属性,您的类将在您单击包的任何按钮时加载。

    我希望我有所帮助。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-10
    • 1970-01-01
    • 2012-09-08
    • 1970-01-01
    • 2020-04-25
    • 2021-05-10
    • 1970-01-01
    • 2016-11-21
    相关资源
    最近更新 更多