C# 如何检查my VS 2017 extension for中的工具窗口是否隐藏

C# 如何检查my VS 2017 extension for中的工具窗口是否隐藏,c#,visual-studio-2017,vsix,visual-studio-sdk,vssdk,C#,Visual Studio 2017,Vsix,Visual Studio Sdk,Vssdk,我正在为Visual Studio 2017开发一个扩展,其中包含自定义的“工具窗口”。此“工具窗口”包含WPF控件,其中视图模型订阅到工作区。工作空间已更改和EnvDTE.DTE.Events.WindowEvents.WindowActivated事件 我知道当用户关闭“工具窗口”时,它实际上并没有被破坏,而是被“隐藏”。然而,它仍然对我的事件作出反应 所以,我想知道两个问题的答案: 如何检查工具窗口是否隐藏 我是否可以“关闭”工具窗口,以便将其销毁 编辑:创建工具窗口的代码: protec

我正在为Visual Studio 2017开发一个扩展,其中包含自定义的“工具窗口”。此“工具窗口”包含
WPF控件
,其中
视图模型
订阅到工作区。工作空间已更改EnvDTE.DTE.Events.WindowEvents.WindowActivated事件

我知道当用户关闭“工具窗口”时,它实际上并没有被破坏,而是被“隐藏”。然而,它仍然对我的事件作出反应

所以,我想知道两个问题的答案:

  • 如何检查工具窗口是否隐藏
  • 我是否可以“关闭”工具窗口,以便将其销毁
  • 编辑:创建工具窗口的代码:

    protected virtual TWindow OpenToolWindow()
    {
            ThreadHelper.ThrowIfNotOnUIThread();
    
            // Get the instance number 0 of this tool window. This window is single instance so this instance
            // is actually the only one.
            // The last flag is set to true so that if the tool window does not exists it will be created.
            ToolWindowPane window = Package.FindToolWindow(typeof(TWindow), id: 0, create: true);
    
            if (window?.Frame == null)
            {
                throw new NotSupportedException("Cannot create tool window");
            }
    
            IVsWindowFrame windowFrame = (IVsWindowFrame)window.Frame;
            Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(windowFrame.Show());
            return window as TWindow;
    }
    

    要检测toolwindow何时关闭,您可以从OnShow方法继承它,并在OnShow方法中检查fShow==(int)。

    只需添加到@Sergey Vlasov的答案中-我发现了另一个方法,如果窗口被隐藏/显示,则会收到通知。下面是我的WPF控件视图模型中的代码

    EnvDTE.DTE dte = MyVSPackage.Instance.GetService<EnvDTE.DTE>();
    
    // _visibilityEvents is a private field. 
    // There is a recommendation to store VS events objects in a field 
    // to prevent them from being GCed
    _visibilityEvents = (dte?.Events as EnvDTE80.Events2)?.WindowVisibilityEvents;
    
    if (_visibilityEvents != null)
    {
        _visibilityEvents.WindowShowing += VisibilityEvents_WindowShowing;
        _visibilityEvents.WindowHiding += VisibilityEvents_WindowHiding;
    }
    
    EnvDTE.DTE-DTE=MyVSPackage.Instance.GetService();
    //\u visibilityEvents是一个私有字段。
    //建议在字段中存储VS事件对象
    //防止他们被GCD录取
    _visibilityEvents=(dte?.Events作为EnvDTE80.Events2)?.WindowVisibilityEvents;
    如果(_visibilityEvents!=null)
    {
    _visibilityEvents.WindowsShowing+=visibilityEvents\u WindowsShowing;
    _visibilityEvents.WindowHiding+=visibilityEvents\u WindowHiding;
    }
    
    你能告诉我们你用来创建窗口的代码吗?@BradleyUffner我已经添加了代码,非常感谢。实际上,我通过订阅EnvDTE80.WindowVisibilityEvents窗口显示/窗口隐藏找到了另一种方法。这种方法有什么陷阱吗?@SENya有趣!我个人没有处理WindowVisibilityEvents的经验。