在VS 2010 RC扩展中找到给定ProjectItem的IVsTextView或IWpfTextView [英] Find an IVsTextView or IWpfTextView for a given ProjectItem, in VS 2010 RC extension

查看:98
本文介绍了在VS 2010 RC扩展中找到给定ProjectItem的IVsTextView或IWpfTextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有ProjectItem,并且想要获得与其关联的IWPFTextView(如果有的话)。

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

我尝试获取IVsTextManager,然后进行迭代视图,但iVsTextManager.EnumViews始终不返回任何内容。

I have tried to get an IVsTextManager, and then iterate through the views, but iVsTextManager.EnumViews always returns nothing.

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

Here is what I've got so far:

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.

肯定还有另一种/更好的方式?

Surely there is another/better way??

预先感谢

〜卡梅隆

推荐答案

怎么做。首先获取项目项的完整路径,然后调用此帮助程序方法:

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

这篇关于在VS 2010 RC扩展中找到给定ProjectItem的IVsTextView或IWpfTextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆