Find an IVsTextView or IWpfTextView for a given ProjectItem, in VS 2010 RC extension

前端 未结 3 1822
一向
一向 2020-12-05 19:46

I have the ProjectItem, and want to get the IWPFTextView that is associated with it, if any.

I have tried to get an IVsTextManager, and then iterate through the view

相关标签:
3条回答
  • 2020-12-05 20:25

    I know it has been a while since this question has been answered, but hopefully the following code for retrieving the IWpfTextView from the IVsTextView will help someone else:

    private IWpfTextView GetWpfTextView(IVsTextView vTextView)
    {
        IWpfTextView view = null;
        IVsUserData userData = vTextView as IVsUserData;
    
        if (null != userData)
        {
            IWpfTextViewHost viewHost;
            object holder;
            Guid guidViewHost = DefGuidList.guidIWpfTextViewHost;
            userData.GetData(ref guidViewHost, out holder);
            viewHost = (IWpfTextViewHost)holder;
            view = viewHost.TextView;
        }
    
        return view;
    }
    
    0 讨论(0)
  • 2020-12-05 20:29

    You can get IWpfTextView like that:

    var componentModel = (IComponentModel)GetService(typeof(SComponentModel));
    var textManager = (IVsTextManager)Package.GetGlobalService(typeof(SVsTextManager));
    IVsTextView activeView = null;
    ErrorHandler.ThrowOnFailure(textManager.GetActiveView(1, null, out activeView));
    var editorAdapter = componentModel.GetService<IVsEditorAdaptersFactoryService>();
    IWpfTextView wpfTextView = editorAdapetr.GetWpfTextView(activeView);
    
    0 讨论(0)
  • 2020-12-05 20:49

    Here is how to do it. First get the full path for the project item, then call this helper method:

    /// <summary>
    /// Returns an IVsTextView for the given file path, if the given file is open in Visual Studio.
    /// </summary>
    /// <param name="filePath">Full Path of the file you are looking for.</param>
    /// <returns>The IVsTextView for this file, if it is open, null otherwise.</returns>
    internal static Microsoft.VisualStudio.TextManager.Interop.IVsTextView GetIVsTextView(string filePath)
    {
        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.Text.Editor.IWpfTextView wpfTextView = null;
        if (Microsoft.VisualStudio.Shell.VsShellUtilities.IsDocumentOpen(serviceProvider, filePath, Guid.Empty,
                                        out uiHierarchy, out itemID, out windowFrame))
        {
            // Get the IVsTextView from the windowFrame.
            return Microsoft.VisualStudio.Shell.VsShellUtilities.GetTextView(windowFrame);
        }
    
        return null;
    }
    
    0 讨论(0)
提交回复
热议问题