2010-03-09 9 views
12

Ben ProjectItem var ve varsa onunla ilişkili IWPFTextView almak istiyorum.Belirli bir ProjectItem için bir IVsTextView veya IWpfTextView bulun, VS 2010 RC uzantısında

Bir IVsTextManager almaya çalıştım ve sonra görünümleri yinelemeye çalıştım, ancak iVsTextManager.EnumViews her zaman hiçbir şey döndürmez. İşte

Ben şimdiye kadar ne var ise:

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. 

Şüphesiz orada başka/daha iyi bir yoldur ??

~ Cameron İşte

cevap

21

bunu nasıl olduğunu önceden

teşekkürler. Ben bu soruya cevap beri bir süre oldu biliyorum ama umarım IVsTextView dan IWpfTextView almak için aşağıdaki kod olacak

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

Ve wpfTextView nasıl alabilirim ?. başkası yardımcı Teşekkürler! – Morvader

+0

Harika. Bu yardımcı yöntem için teşekkürler. Benzer problemi çözmek için bana çok yardımcı oldum. –

+0

Metodunu kullanarak, kendim için çözdüğüm şeyi burada bulabilirsiniz: http://stackoverflow.com/a/24178352/395069 –

12

: Öncelikle proje öğesi için tam yol almak, o zaman bu yardımcı yöntemini çağırmak

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

böyle IWpfTextView alabilirsiniz:

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

Bu kodun aslında% 100 çalışmadığını gördüm. Bir .sql dosyasında deneyin. Seçilen cevap benim için daha tutarlı bir şekilde çalıştı. – kman

İlgili konular