我有兴趣获取当前选择的Project或ProjectItem(作为示例,不限于这两个),其中只选择了一个项目。
大多数人似乎使用IVsMonitorSelection
来获取IVSHierarchy
,然后使用以下内容获取所选项目的对象(如果选择了单个项目):
var monitorSelection = (IVsMonitorSelection) Package.GetGlobalService(typeof(IVsMonitorSelection));
IntPtr hierarchyPointer, selectionContainerPointer;
uint projectItemId;
IVsMultiItemSelect multiItemSelect;
monitorSelection.GetCurrentSelection(out hierarchyPointer, out projectItemId, out multiItemSelect, out selectionContainerPointer);
var hierarchy = (IVsHierarchy) Marshal.GetObjectForIUnknown(hierarchyPointer);
Marshal.Release(hierarchyPointer);
Marshal.Release(selectionContainerPointer);
object o;
hierarchy.GetProperty((uint) projectItemId, (int) __VSHPROPID.VSHPROPID_ExtObject, out o);
但是,GetProperty
在此返回E_NOTIMPL。我使用了错误的参数吗?是否还有替代解决方案?
答案 0 :(得分:3)
您可以像这样使用 dte.ToolWindows.SolutionExplorer.SelectedItems :
EnvDTE.ProjectItem projectItem = GetSelectedSolutionExplorerItem().Object as EnvDTE.ProjectItem;
private EnvDTE.UIHierarchyItem GetSelectedSolutionExplorerItem()
{
EnvDTE.UIHierarchy solutionExplorer = dte.ToolWindows.SolutionExplorer;
object[] items = solutionExplorer.SelectedItems as object[];
if (items.Length != 1)
return null;
return items[0] as EnvDTE.UIHierarchyItem;
}
答案 1 :(得分:2)
根据谢尔盖的答案,我找到了dte.SelectedItems,它甚至更强烈地输入了#34;并且不需要强制转换为包含UIHierarchy的数组 项目
结果现在是:
dte.SelectedItems.Item(1).ProjectItem