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

发布于 2024-08-24 16:51:56 字数 748 浏览 7 评论 0原文

我有 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.

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

预先感谢

〜卡梅伦

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

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

Thanks in advance

~ Cameron

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

撩发小公举 2024-08-31 16:51:57

以下是具体操作方法。首先获取项目项的完整路径,然后调用此帮助器方法:

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

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;
}
夜夜流光相皎洁 2024-08-31 16:51:57

我知道这个问题已经有一段时间了,但希望以下用于从 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;
}

I know it has been a while since this question has been answered, but hopefully the following code for retrieving the IWpfTextView from the IVsTextView will help someone else:

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;
}
听不够的曲调 2024-08-31 16:51:57

你可以这样获取 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);

You can get IWpfTextView like that:

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文