2010-03-09 8 views
12

Tengo ProjectItem y quiero obtener el IWPFTextView que está asociado con él, si lo hay.Encuentre un IVsTextView o IWpfTextView para un ProjectItem dado, en la extensión VS 2010 RC

He intentado obtener un IVsTextManager, y luego recorrer las vistas, pero iVsTextManager.EnumViews siempre devuelve nada.

Aquí es lo que tengo hasta ahora:

var txtMgr = (IVsTextManager)Package.GetGlobalService(typeof(SVsTextManager)); 

if (txtMgr != null) 
{ 
    IVsEnumTextViews iVsEnumTextViews; 
    IVsTextView[] views = null; 

    // Passing null will return all available views, at least according to the documentation 
    // unfortunately, this returns a 0x80070057 error (invalid parameter) 
    var errorValue = txtMgr.EnumViews(null, out iVsEnumTextViews); 

    if (errorValue == VSConstants.S_OK) 
    { 
     // enumerate, find the IVsTextView with a matching filename. 

Seguramente hay otro/mejor manera ??

Gracias de antemano

~ Cameron

Respuesta

21

Aquí es cómo hacerlo. En primer lugar obtener la ruta completa para el elemento de proyecto, a continuación, llamar a este método de ayuda:

/// <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; 
} 
+1

Y ¿cómo se consigue la wpfTextView ?. ¡Gracias! – Morvader

+0

Genial. Gracias por este método de ayuda. Realmente me ayudó mucho para resolver un problema similar. –

+0

Usando su método, esto es lo que resolví por mí mismo: http://stackoverflow.com/a/24178352/395069 –

12

Sé que ha sido un tiempo desde que esta pregunta ha sido contestada, pero espero que el siguiente código para recuperar el IWpfTextView del IVsTextView haré ayudar a alguien más:

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

usted puede obtener IWpfTextView así:

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

De hecho, he visto que este código no funciona el 100% del tiempo. Pruébalo en un archivo .sql. La respuesta seleccionada ha funcionado de manera más consistente para mí. – kman

Cuestiones relacionadas