如何检查我的 VS 2017 扩展中的工具 window 是否被隐藏
How to check that the tool window in my VS 2017 extension for is hidden
我正在为 Visual Studio 2017 开发一个包含自定义 "toolwindow" 的扩展。此 "toolwindow" 包含 WPF control
,其中 view model
订阅了 Workspace.WorkspaceChanged 和 EnvDTE.DTE.Events.WindowEvents.WindowActivated 事件。
我知道当 "toolwindow" 被用户关闭时,它实际上并没有被销毁,而是 "hidden"。但是,它仍然对我的事件做出反应。
所以,我想知道两个问题的答案:
- 如何查看工具window是否被隐藏?
- 我可以 "close" 工具 window 这样它就会被销毁吗?
编辑:创建工具的代码window:
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;
}
要检测工具窗口何时关闭,您可以继承自 IVsWindowFrameNotify3 and in the OnShow method check for fShow == (int)__FRAMESHOW.FRAMESHOW_WinClosed。
只是为了添加到@Sergey Vlasov 的回答中——我发现了第二种方法,如果 window 是 hidden/shown,就会收到通知。这是我的 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;
}
我正在为 Visual Studio 2017 开发一个包含自定义 "toolwindow" 的扩展。此 "toolwindow" 包含 WPF control
,其中 view model
订阅了 Workspace.WorkspaceChanged 和 EnvDTE.DTE.Events.WindowEvents.WindowActivated 事件。
我知道当 "toolwindow" 被用户关闭时,它实际上并没有被销毁,而是 "hidden"。但是,它仍然对我的事件做出反应。
所以,我想知道两个问题的答案:
- 如何查看工具window是否被隐藏?
- 我可以 "close" 工具 window 这样它就会被销毁吗?
编辑:创建工具的代码window:
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;
}
要检测工具窗口何时关闭,您可以继承自 IVsWindowFrameNotify3 and in the OnShow method check for fShow == (int)__FRAMESHOW.FRAMESHOW_WinClosed。
只是为了添加到@Sergey Vlasov 的回答中——我发现了第二种方法,如果 window 是 hidden/shown,就会收到通知。这是我的 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;
}