如何关闭一个IWpfTextView基于条件从IWpfTextViewCreationListener - VsText

本文关键字:条件 IWpfTextViewCreationListener VsText 于条件 IWpfTextView 何关闭 一个 | 更新日期: 2023-09-27 18:06:33

我有一个类监听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;
                }
            }
        }
    }
}

在函数- VsTextViewCreated中,基于一定的条件,我想关闭textview。

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

然而,在这样做之后,我得到以下错误

系统。ObjectDisposedException was unhandled消息:一个未处理的类型'System '的异常。ObjectDisposedException'发生在Microsoft.VisualStudio.Platform.VSEditor.dll无法访问已处置的对象。

我也试过

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

但这也没有帮助。什么可能是正确的方式来关闭编程的textview ?

如何关闭一个IWpfTextView基于条件从IWpfTextViewCreationListener - VsText

好的…我能够解决以上问题…

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

但是,我仍然想知道为什么不IWpfTextView.Close()或IVSTextView.CloseView()不工作…