2010-03-09 7 views
12

Ho l'oggetto ProjectItem e desidero ottenere l'IWPFTextView associato, se presente.Trova un IVsTextView o IWpfTextView per un determinato oggetto ProjectItem, nell'estensione VS 2010 RC

Ho provato a ottenere un IVsTextManager e quindi a scorrere le viste, ma iVsTextManager.EnumViews non restituisce mai nulla.

Ecco quello che ho finora:

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. 

Sicuramente c'è un altro modo/meglio ??

Grazie in anticipo

~ Cameron

risposta

21

Ecco come farlo. In primo luogo ottenere il percorso completo per la voce del progetto, quindi chiamare questo metodo di supporto:

/// <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

E come si fa a ottenere il wpfTextView ?. Grazie! – Morvader

+0

Grande. Grazie per questo metodo di supporto. Mi ha davvero aiutato molto a risolvere un problema simile. –

+0

Utilizzando il tuo metodo, ecco cosa ho risolto da solo: http://stackoverflow.com/a/24178352/395069 –

12

So che è stato un po 'che questa domanda è stato risposto, ma si spera che il seguente codice per recuperare l'IWpfTextView dal IVsTextView farò aiutare qualcun altro:

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

È possibile ottenere IWpfTextView così:

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

In realtà ho visto questo codice non funzionare il 100% delle volte. Provalo su un file .sql. La risposta selezionata ha funzionato in modo più coerente per me. – kman

Problemi correlati