Visual Studio 加载项:无法从 SelectedItems 获取 Document 对象
首先我要说的是,我是 Visual Studio 开发插件的新手...
当使用 DTE2 对象时,我试图获取有关当前所选文档的一些信息。如果我这样做:
DTE2 VisualStudioInstance = somethingOrOther;
Document documentInfo;
if(VisualStudioInstance.SelectedItems.MultiSelect == false)
{
documentInfo = VisualStudioInstance.SelectedItems.Item(1).ProjectItem.Document;
}
Document 对象似乎总是为空。 VisualStudioInstance.SelectedItems.Item(1).Project 也是 null (我不一定需要,但这似乎也很奇怪。它确实给了我所选项目的名称,所以我至少知道它正在寻找一些东西 现在,
如果我直接转到 VisualStudioInstance.ActiveDocument,我会获得所需的所有信息。是否确实必须打开文档才能获取信息?如果是这样,我还可以如何获取所选项目的文档信息?我要查找的主要内容是所选文件的完整路径信息。
Let me start by saying I'm new to developing add-ins for visual studio...
When using the DTE2 object, I am attempting to get some info on the document that is currently selected. If I do something like this:
DTE2 VisualStudioInstance = somethingOrOther;
Document documentInfo;
if(VisualStudioInstance.SelectedItems.MultiSelect == false)
{
documentInfo = VisualStudioInstance.SelectedItems.Item(1).ProjectItem.Document;
}
The Document object always seems to be null. VisualStudioInstance.SelectedItems.Item(1).Project is also null (which I don't necessarily need, but that seems odd as well. It does give me the name of the selected item, so I know at least that it is finding something.
Now if I go directly to VisualStudioInstance.ActiveDocument, I get all the info I am looking for. Does the document actually have to be open to get the information? If so, how else would I go about getting document info for a selected item without opening it? The main thing I'm looking for is full path information for the selected file. Thanks in advance.
实际上你可以使用 ProjectItem.FileNames 属性。它是一个索引属性,即使文档关闭也有效。
Actually you can use ProjectItem.FileNames property. It is an indexed property which is valid even if the document is closed.