简体   繁体   English

菜单项的动态可见性

[英]Dynamic Visibility of menu item

In my VS extension I need to add menu item for my new project type.在我的 VS 扩展中,我需要为我的新项目类型添加菜单项。 But I want it to show for my custom type only.但我希望它只显示我的自定义类型。 So I added this code to.vcst file:所以我将此代码添加到 .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>

And added this code to package initialization:并将此代码添加到 package 初始化中:

            // 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:我还尝试在我的 package 上实现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;

But that doesn't helped either.但这也无济于事。 Method is called, but setting OLECMDF.OLECMDF_INVISIBLE does nothing.方法被调用,但设置OLECMDF.OLECMDF_INVISIBLE什么都不做。 What should I do to hide this menu item for unsupported menu items?对于不受支持的菜单项,我应该怎么做才能隐藏此菜单项?

Probably the problem is related with the load of the Package. 可能问题与包的负载有关。 To load the Package automatically just add this attribute to your Package Class: 要自动加载Package,只需将此属性添加到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. 我希望我有所帮助。

In case it's applicable, Microsoft recommends using rule-based UI contexts when auto-loading packages:如果适用,Microsoft 建议在自动加载包时使用基于规则的 UI 上下文

Loading packages can have a performance impact and loading them sooner than needed is not the best practice.加载包可能会对性能产生影响,并且比需要更快地加载它们不是最佳实践。 Visual Studio 2015 introduced the concept of Rules-based UI Contexts, a mechanism that allows extension authors to define the precise conditions under which a UI Context is activated and associated VSPackages are loaded. Visual Studio 2015 引入了基于规则的 UI 上下文的概念,这种机制允许扩展作者定义激活 UI 上下文和加载关联 VSPackage 的精确条件。

I've only ever used this strategy to show menu items for specific file types (here's an example of how simple that can be), but there are also some term types related to projects that might be more useful for this scenario.我只使用过这种策略来显示特定文件类型的菜单项(这是一个简单的 示例),但也有一些与项目相关的术语类型可能对这种情况更有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 更改 blazor 菜单中导航项的可见性 - Change visibility of nav item in blazor menu Flipview的项目模板绑定可见性的动态变化 - Dynamic change of binded visibility of flipview's item template WPF ResourceDictionary上下文菜单-菜单项的可见性属性从其他类更新 - WPF ResourceDictionary context menu - menu item visibility property update from other class WPF单击动态菜单项并返回名称? - WPF Clicking on a dynamic menu item and returning the name? ObservableCollection中的动态菜单:将项目作为参数传递 - Dynamic Menu from ObservableCollection: Passing Item as parameter 具有可见性权限的WPF菜单 - WPF Menu with visibility permission 将可见性绑定到可检查菜单项会在WPF中显示错误“服务提供程序缺少INameResolver服务” - Bind visibility to checkable menu item shows error “Service provider is missing the INameResolver service” in WPF Visual Studio包:设置自定义解决方案资源管理器上下文菜单项的可见性 - Visual Studio Package: Settings the visibility of a custom Solution Explorer context menu item 根据索引加载后的条件更改 Blazor 菜单中导航项的可见性 - Change visibility of nav item in blazor menu based on condition after index load 更改列表框项目的可见性 - Change ListBox item visibility
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM