首页 > 解决方案 > 仅在代码窗口打开时启用我的 Visual Studio 扩展的菜单项

问题描述

我只想在代码编辑器打开时启用我的扩展菜单项。

我尝试检测何时使用以下代码打开代码窗口而没有成功结果...

private void MenuItem_BeforeQueryStatus(object sender, EventArgs e)
    {
        OleMenuCommand menuCommand = sender as OleMenuCommand;
        if (null != menuCommand)
        {
            IVsMonitorSelection monitorSelection =  this.ServiceProvider.GetService(typeof(IVsMonitorSelection)) as IVsMonitorSelection;
            int pfActive;

            Guid codeWindowGuid = VSConstants.UICONTEXT_CodeWindow;
            uint solutionExistCookie;
            monitorSelection.GetCmdUIContextCookie(ref codeWindowGuid, out solutionExistCookie);
            monitorSelection.IsCmdUIContextActive(solutionExistCookie, out pfActive);

            menuCommand.Enabled = (pfActive == 1);
        }
    }

虽然IsCmdUIContextActive正在恢复S_OK,但即使代码窗口确实打开了,我也总是将 pfActive 接收为 false ..

我究竟做错了什么 ?

标签: c#visual-studiovisual-studio-extensions

解决方案


也许您忘记在方法签名中定义的地方添加out关键字?IsCmdUIContextActive


推荐阅读