在VS 2010 RC扩展中查找给定ProjectItem的IVsTextView或IWpfTextView

时间:2010-03-09 23:56:31

标签: visual-studio-2010 add-in

我有ProjectItem,想要获取与之关联的IWPFTextView(如果有的话)。

我试图获取一个IVsTextManager,然后遍历视图,但iVsTextManager.EnumViews总是不返回任何内容。

这是我到目前为止所得到的:

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.

当然还有另一种/更好的方法吗?

提前致谢

~Cameron

3 个答案:

答案 0 :(得分:23)

以下是如何做到这一点。首先获取项目项的完整路径,然后调用此辅助方法:

/// <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 :(得分:13)

我知道已经有一段时间了,因为这个问题已得到解答,但希望以下代码从IVsTextView中检索IWpfTextView可以帮助其他人:

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

答案 2 :(得分:0)

你可以这样得到IWpfTextView:

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