gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 22:52:48 25 4
gpt4 key购买 nike

我有 ProjectItem,并希望获得与其关联的 IWPFTextView(如果有)。

我试图获得一个 IVsTextManager,然后遍历 View ,但 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.

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

提前致谢

~ 卡梅伦

最佳答案

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

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

关于visual-studio-2010 - 在 VS 2010 RC 扩展中为给定的 ProjectItem 查找 IVsTextView 或 IWpfTextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2413530/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com