locked
How to close a IWpfTextView based on a condition from the IWpfTextViewCreationListener - VsTextViewCreated RRS feed

  • Question

  • Hi,

    I have a class which listens to the IWpfTextViewCreationListener 

    using System.ComponentModel.Composition;
    using Microsoft.VisualStudio;
    using Microsoft.VisualStudio.Editor;
    using Microsoft.VisualStudio.Shell;
    using Microsoft.VisualStudio.Text.Editor;
    using Microsoft.VisualStudio.TextManager.Interop;
    using Microsoft.VisualStudio.Utilities;
    using Microsoft.VisualStudio.OLE.Interop;
    
    namespace GoToSequence
    {
        [Export(typeof(IVsTextViewCreationListener))]
        [ContentType("Code")]
        [TextViewRole(PredefinedTextViewRoles.Editable)]
        internal class GoToSequenceEditorCreationListener : IVsTextViewCreationListener
        {
            [Import(typeof(IVsEditorAdaptersFactoryService))]
            internal IVsEditorAdaptersFactoryService editorFactory = null;
    
            public void VsTextViewCreated(IVsTextView textViewAdapter)
            {
                IWpfTextView textView = editorFactory.GetWpfTextView(textViewAdapter);
                if (textView == null)
                    return;
    
                AddCommandFilter(textViewAdapter, new GoToSequenceCommandHandler(textView, textViewAdapter));
            }
    
            void AddCommandFilter(IVsTextView viewAdapter, GoToSequenceCommandHandler commandFilter)
            {
                if (commandFilter.m_added == false)
                {
                    //get the view adapter from the editor factory
                    IOleCommandTarget next;
                    int hr = viewAdapter.AddCommandFilter(commandFilter, out next);
    
                    if (hr == VSConstants.S_OK)
                    {
                        commandFilter.m_added = true;
                        //you'll need the next target for Exec and QueryStatus 
                        if (next != null)
                            commandFilter.m_nextTarget = next;
                    }
                }
            }
        }
    }

    In the function - VsTextViewCreated, based on a certain condition I want to close the textview. 

    If (condition == null)
       IWpfTextView.Close()


    However after doing this I get the following error

    System.ObjectDisposedException was unhandled
    Message: An unhandled exception of type 'System.ObjectDisposedException' occurred in Microsoft.VisualStudio.Platform.VSEditor.dll
    Additional information: Cannot access a disposed object.

    I also tried 

    if(condition == null)
      IVsTextView.CloseView()

    but this also did not help. What could be the correct way to close the textview programmatically?


    • Edited by kaveeshd Thursday, August 6, 2015 6:11 AM
    Thursday, August 6, 2015 4:27 AM

Answers

  • Okay... I was able to resolve the above...

    var dte2 = (EnvDTE80.DTE2)Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(Microsoft.VisualStudio.Shell.Interop.SDTE));
    
    Microsoft.VisualStudio.OLE.Interop.IServiceProvider sp = (Microsoft.VisualStudio.OLE.Interop.IServiceProvider)dte2;
    
    Microsoft.VisualStudio.Shell.ServiceProvider serviceProvider = new Microsoft.VisualStudio.Shell.ServiceProvider(sp);
    
    Microsoft.VisualStudio.Shell.Interop.IVsUIHierarchy uiHierarchy;
    
    uint itemID;
    
    Microsoft.VisualStudio.Shell.Interop.IVsWindowFrame windowFrame;
    
    Microsoft.VisualStudio.Shell.VsShellUtilities.IsDocumentOpen(serviceProvider, _iTextDocument.FilePath, Guid.Empty, out uiHierarchy, out itemID, out windowFrame);
    
    windowFrame.CloseFrame((uint)__FRAMECLOSE.FRAMECLOSE_NoSave);


    But, I still wonder why doesn't the IWpfTextView.Close() or IVSTextView.CloseView() do not work...

    • Marked as answer by kaveeshd Thursday, August 6, 2015 10:12 AM
    Thursday, August 6, 2015 10:12 AM

All replies

  • Okay... I was able to resolve the above...

    var dte2 = (EnvDTE80.DTE2)Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(Microsoft.VisualStudio.Shell.Interop.SDTE));
    
    Microsoft.VisualStudio.OLE.Interop.IServiceProvider sp = (Microsoft.VisualStudio.OLE.Interop.IServiceProvider)dte2;
    
    Microsoft.VisualStudio.Shell.ServiceProvider serviceProvider = new Microsoft.VisualStudio.Shell.ServiceProvider(sp);
    
    Microsoft.VisualStudio.Shell.Interop.IVsUIHierarchy uiHierarchy;
    
    uint itemID;
    
    Microsoft.VisualStudio.Shell.Interop.IVsWindowFrame windowFrame;
    
    Microsoft.VisualStudio.Shell.VsShellUtilities.IsDocumentOpen(serviceProvider, _iTextDocument.FilePath, Guid.Empty, out uiHierarchy, out itemID, out windowFrame);
    
    windowFrame.CloseFrame((uint)__FRAMECLOSE.FRAMECLOSE_NoSave);


    But, I still wonder why doesn't the IWpfTextView.Close() or IVSTextView.CloseView() do not work...

    • Marked as answer by kaveeshd Thursday, August 6, 2015 10:12 AM
    Thursday, August 6, 2015 10:12 AM
  • Hi kaveeshd,

    Closing the text view is different from closing the window frame I think. Anyway, glad you've found the solution.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, August 7, 2015 3:26 AM
  • That is what I wonder...

    When the IWpfTextView is created in the window, the VsTextViewCreated function gets fired. At this instant we do have the IWpfTextView object, but why doesn't calling the Close function work? Could this be a bug?

    Friday, August 7, 2015 7:18 AM