![](/img/trans.png)
[英]How to get the details of the selected item in solution explorer using vs package
[英]In a VS 2015 extension, how can I get the selected object in the Solution Explorer?
我有興趣為當前選擇獲取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;
}
根據Sergey的回答,我找到了dte.SelectedItems,它甚至是“更強類型”的,並且不需要轉換為包含UIHierarchy項的數組。
結果現在是:
dte.SelectedItems.Item(1).ProjectItem
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.