Quantcast
Channel: Visual Studio Integrate forum
Viewing all articles
Browse latest Browse all 4410

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

$
0
0

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 inMicrosoft.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?



Viewing all articles
Browse latest Browse all 4410

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>