在 VS 2015 扩展中,如何在解决方案资源管理器中获取选定的对象?

2024-01-03

我有兴趣为当前选择获取一个 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。我使用了错误的参数吗?也许有替代解决方案吗?


您可以使用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;
    }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 VS 2015 扩展中,如何在解决方案资源管理器中获取选定的对象? 的相关文章

随机推荐