我正在使用Visual Studio程序包,我似乎遇到了IVsInvisibleEditorManager
和Running Document Table(RDT)的问题。
首先,我在普通的Visual Studio编辑器中打开了一个文件。接下来,我通过以下方式为同一个文件注册一个IVsInvisibleEditor:
IVsInvisibleEditor invisibleEditor;
ErrorHandler.ThrowOnFailure(this._InvisibleEditorManager.RegisterInvisibleEditor(
filePath
, pProject: null
, dwFlags: (uint)_EDITORREGFLAGS.RIEF_ENABLECACHING
, pFactory: null
, ppEditor: out invisibleEditor));
当我修改文件并关闭主Visual Studio编辑器时,系统会提示我保存文档。我的理解是,情况并非如此,因为我仍然可以在隐形编辑器中访问此文件。然后,Visual Studio会清除与此文件关联的一些资源,这会破坏我的隐形编辑器。
我怀疑这是因为RegisterInvisibleEditor()
没有在RDT中正确注册我的文档。
RegisterInvisibleEditor()
的文档在使用dwFlags
时说明了_EDITORREGFLAGS.RIEF_ENABLECACHING
的以下内容:
这允许文档在a的场景中保留在RDT中 文档在可见编辑器中打开,并在用户关闭时关闭 隐形编辑器已为该文件注册。
这完全描述了我的问题。可见编辑器正在关闭,但我希望该文档保留在RDT中。
有谁知道如何让我的文档在RDT中持续存在?
RDT项目是否具体?我为pProject和pFactory传入null的事实是否会导致RDT出现任何问题?
修改:我刚刚测试了上面的代码,但是传入了相应的IVsProject
并且没有任何变化。注册一个不可见的编辑器时,RDT仍然没有改变。
答案 0 :(得分:0)
似乎我不能说服IVsInvisibleEditorManager
为文档添加锁定。不幸的是,RegisterInvisibleEditor()
是一种COM方法,这意味着我无法反编译并查看它正在做什么(至少我的知识有限)。
但是,我提出了一种解决方法,我在其中手动管理RDT中的条目。
var rdt =
(IVsRunningDocumentTable)GetService(typeof(SVsRunningDocumentTable));
//Retrieve the appropriate IVsHierarchy. I've assumed there's only
//one project within this solution.
var solutionService = (IVsSolution)GetService(typeof(SVsSolution));
IVsProject project = null;
Guid guid = Guid.Empty;
solutionService.GetProjectEnum((uint)__VSENUMPROJFLAGS.EPF_LOADEDINSOLUTION, ref guid, out enumerator);
var hierarchy = new IVsHierarchy[1] { null };
uint fetched = 0;
for((enumerator.Reset(); enumerator.Next(1, hierarchy, out fetched) == VSConstants.S_OK && fetched == 1;)
hierarchy = hierarchyArray[0]
//Then when creating the IVsInvisibleEditor, find and lock the document
uint itemID;
IntPtr docData;
uint docCookie;
var result = rdt.FindAndLockDocument(
dwRDTLockType: (uint)_VSRDTFLAGS.RDT_ReadLock
, pszMkDocument: filePath
, ppHier: out hierarchy
, pitemid: out itemID
, ppunkDocData: out docData
, pdwCookie: out docCookie
);
在某些时候,您将完成IVsInvisibleEditor,此时您应该从RDT解锁文档。
runningDocTable.UnlockDocument((uint)_VSRDTFLAGS.RDT_ReadLock, docCookie);