Найдите IVsTextView или IWpfTextView для данного ProjectItem, в VS 2010 RC extension
У меня есть 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.
Конечно, есть другой/лучший способ?
Заранее спасибо
~ Камерон
Ответы
Ответ 1
Вот как это сделать. Сначала введите полный путь для элемента проекта, затем вызовите этот вспомогательный метод:
/// <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;
}
Ответ 2
Я знаю, что прошло некоторое время, поскольку на этот вопрос был дан ответ, но, надеюсь, следующий код для получения IWpfTextView из IVsTextView поможет кому-то другому:
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;
}
Ответ 3
Вы можете получить 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);