I have built a VSPackage that projects text from an IVsInvisibleEditor
. This IVsInvisibledEditor
corresponds to a file that the user has opened within Visual Studio and exists within a Document Window.
When the user closes this Document Window, it removes a number of properties from the underlying ITextBuffer
. In particular it removes the ITextDocument
property from the source ITextBuffer
.
I would like to prevent this, as the user may be using my extension to continue editing the document. When the primary document editor is closed, the removal of these properties prevents keystrokes from being recognized within my custom editor.
How can I detect when Visual Studio is closing a document window? I've explored the EnvDTE class and I've noticed the DocumentEvents class, but it's marked as Microsoft-internal.
Is there a public API where I can detect when document windows are closed?
One effective way to know when a Document Window is close is using EnvDTE WindowEvents
WindowEvents _windowEvents;
// EnvDTE.Window _editorWindow=null;
_windowEvents = _vsObject.Events.get_WindowEvents(_editorWindow);
_windowEvents.WindowClosing += OnWindowClosing;
void OnWindowClosing(Window Window)
{
/// your stuff here
}
The other option is trough IVsWindowFrameNotify..IVsWindowFrameNotify3 interfaces
if you have a valid reference to the document IVsWindowFrame you can use the Advise method to receive all frame related events.
_iVsWindowFrame.Advise(hostWindowFrameNotify, out hostWindowFrameNotify_cookie);
Just remember to call IVsWindowFrame.Unadvise to clean up the hook.