我有一个使用新的VS扩展API的托管语法高亮笔,它给了我一个ITextBuffer,这很棒。
在我的扩展的另一部分中,我获取了一个DTE对象,并将其附加到活动窗口更改事件,这给了我一个EnvDTE.Window对象。
var dte = (EnvDTE.DTE)this.GetService(typeof(EnvDTE.DTE));
dte.Events.WindowEvents.WindowActivated += WindowEvents_WindowActivated;
// ...
private void WindowEvents_WindowActivated(EnvDTE.Window GotFocus, EnvDTE.Window LostFocus)
{
// ???
// Profit
}我想用这个方法把ITextBuffer从窗口中拿出来。有人能告诉我一种直接的方法吗?
发布于 2011-09-11 01:30:19
我使用的解决方案是获取Windows路径,然后将其与IVsEditorAdaptersFactoryService和VsShellUtilities结合使用。
var openWindowPath = Path.Combine(window.Document.Path, window.Document.Name);
var buffer = GetBufferAt(openWindowPath);和
internal ITextBuffer GetBufferAt(string filePath)
{
var componentModel = (IComponentModel)GetService(typeof(SComponentModel));
var editorAdapterFactoryService = componentModel.GetService<IVsEditorAdaptersFactoryService>();
var serviceProvider = new Microsoft.VisualStudio.Shell.ServiceProvider(MetaSharpPackage.OleServiceProvider);
IVsUIHierarchy uiHierarchy;
uint itemID;
IVsWindowFrame windowFrame;
if (VsShellUtilities.IsDocumentOpen(
serviceProvider,
filePath,
Guid.Empty,
out uiHierarchy,
out itemID,
out windowFrame))
{
IVsTextView view = VsShellUtilities.GetTextView(windowFrame);
IVsTextLines lines;
if (view.GetBuffer(out lines) == 0)
{
var buffer = lines as IVsTextBuffer;
if (buffer != null)
return editorAdapterFactoryService.GetDataBuffer(buffer);
}
}
return null;
}https://stackoverflow.com/questions/7184857
复制相似问题