我在我正在构建的扩展中使用这段代码(从这里看到):
IVsUIShellOpenDocument openDoc = Package.GetGlobalService(typeof(IVsUIShellOpenDocument)) as IVsUIShellOpenDocument;
if (openDoc == null) {
return;
}
IVsWindowFrame frame;
Microsoft.VisualStudio.OLE.Interop.IServiceProvider sp;
IVsUIHierarchy hier;
uint itemid;
Guid logicalView = VSConstants.LOGVIEWID_Code;
if (ErrorHandler.Failed(openDoc.OpenDocumentViaProject(path, ref logicalView, out sp, out hier, out itemid, out frame)) || frame == null) {
return;
}
object docData;
frame.GetProperty((int)__VSFPROPID.VSFPROPID_DocData, out docData);
// Get the VsTextBuffer
VsTextBuffer buffer = docData as VsTextBuffer;
if (buffer == null) {
IVsTextBufferProvider bufferProvider = docData as IVsTextBufferProvider;
if (bufferProvider != null) {
IVsTextLines lines;
ErrorHandler.ThrowOnFailure(bufferProvider.GetTextBuffer(out lines));
buffer = lines as VsTextBuffer;
Debug.Assert(buffer != null, "IVsTextLines does not implement IVsTextBuffer");
if (buffer == null) {
return;
}
}
}
// Finally, perform the navigation.
IVsTextManager mgr = Package.GetGlobalService(typeof(VsTextManagerClass)) as IVsTextManager;
if (mgr == null) {
return;
}
mgr.NavigateToLineAndColumn(buffer, ref logicalView, line, column, line, column);
哪个(根据帖子)应该打开我的文档(如果还没有的话),然后聚焦文档,将光标放在给定的位置。虽然这一切都有效,但似乎不起作用的是文档的焦点,我的 ToolWindow 仍然具有活动焦点(即黄色突出显示的标题栏),我尝试将这一行放在NavigateToLineAndColumn
, 之后看看它是否做了什么我期待,但仍然没有骰子:
frame.Show()
根据MSDN
使该窗口可见,将窗口置于顶部,并激活该窗口。
我还需要做些什么,才能使打开的文档更加活跃?