如何检查我的VS 2017扩展程序中的工具窗口是否隐藏

时间:2019-04-01 14:53:28

标签: c# visual-studio-2017 vsix visual-studio-sdk vssdk

我正在为Visual Studio 2017开发一个扩展,其中包含自定义“工具窗口”。此“工具窗口”包含WPF control,其中view model订阅了 Workspace.WorkspaceChanged EnvDTE.DTE.Events.WindowEvents.WindowActivated 事件。 / p>

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

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

  1. 如何检查工具窗口是否隐藏?
  2. 我可以“关闭”工具窗口,以便将其销毁吗?

编辑:创建工具窗口的代码:

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

2 个答案:

答案 0 :(得分:2)

要检测何时关闭工具窗口,可以从IVsWindowFrameNotify3继承它,然后在OnShow方法中检查fShow ==(int)__FRAMESHOW.FRAMESHOW_WinClosed

答案 1 :(得分:0)

只需添加到@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;
}