本文介绍了在 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.
这是我目前得到的:
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!