简体   繁体   English

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

[英]In a VS 2015 extension, how can I get the selected object in the Solution Explorer?

I'm interested in getting a Project or ProjectItem (as examples, not limited to those two) for the current selection where only a single item is selected. 我有兴趣为当前选择获取Project或ProjectItem(作为示例,不限于这两个),其中仅选择单个项目。

Most people seem to be using IVsMonitorSelection to get an IVSHierarchy and then use the following to get the object for the selected item (in case of a single item being selected): 大多数人似乎使用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);

However, GetProperty returns E_NOTIMPL here. 但是, GetProperty在此返回E_NOTIMPL。 Am I using the wrong parameters? 我使用了错误的参数吗? Is there an alternative solution perhaps? 是否有替代解决方案?

You can use dte.ToolWindows.SolutionExplorer.SelectedItems like this: 您可以像这样使用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;
    }

Based on the answer from Sergey, I found dte.SelectedItems, which is even "more strongly-typed" and does not require casting to an array containing UIHierarchy items. 根据Sergey的回答,我找到了dte.SelectedItems,它甚至是“更强类型”的,并且不需要转换为包含UIHierarchy项的数组。

The result is now: 结果现在是:

dte.SelectedItems.Item(1).ProjectItem

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用vs包在解决方案资源管理器中获取所选项目的详细信息 - How to get the details of the selected item in solution explorer using vs package vs 2015中将解决方案资源管理器切换为编辑器 - switch solution explorer to editor in vs 2015 如何在VS 2015中获取所选的错误描述 - how to get the selected error description in VS 2015 在我的 VS 扩展中,如何在 Visual Studio 2015/2017 中以编程方式预览文件? - In my VS extension, how can I programmatically preview a file in Visual Studio 2015/2017? Visual Studio 2015解决方案资源管理器操作:UIHierarchy与IVsHierarchy - Visual Studio 2015 solution explorer manipulation: UIHierarchy vs IVsHierarchy 在解决方案资源管理器中选择命令时,如何知道选择了哪个文件 - How do I know what file was selected when the command was selected in the Solution Explorer 如何引导VSIX VS2015扩展 - How to Bootstrap an VSIX VS2015 Extension 如何获得扩展方法来更改原始对象? - How can I get an extension method to change the original object? 如何在安装vs2015之后在vs2015上安装Asp.Net? - How can I install Asp.Net on vs2015 after already installing vs2015 ? 如何使用 DTE 从 VS 2015/2017 的错误列表中获取错误代码? 或者其他方式可以获得错误代码? - How can I get the Error Code from Error List in VS 2015/2017 with DTE? Or other ways can get error code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM