我正在为 Visual Studio 2010 (vsix) 编写自定义包。
我需要做的是将上下文菜单按钮添加到解决方案资源管理器中的项目节点。
我已经设法在右键单击项目节点时显示上下文菜单,但我的下一个挑战是获取对已单击的项目对象的引用。目前,我可以使用下面的代码通过 IDE 中的事件文档来获取项目。
DTE dte = (DTE)ServiceProvider.GlobalProvider.GetService(typeof(DTE));
Project project = dte.ActiveDocument.ProjectItem.ContainingProject;
所以我的问题是:如何获得对在解决方案资源管理器中选择的项目的类似引用?
最佳答案
我想通了。不妨分享一下信息。
通过使用 SVsShellMonitorSelection
服务,我可以获得对选定层次结构的引用作为 IVsHierarchy
,这反过来又允许我获得对选定对象的引用。然后可以根据在解决方案资源管理器中选择的内容将其转换为 Project
、ProjectItem
等类。方便!
IntPtr hierarchyPointer, selectionContainerPointer;
Object selectedObject = null;
IVsMultiItemSelect multiItemSelect;
uint projectItemId;
IVsMonitorSelection monitorSelection =
(IVsMonitorSelection)Package.GetGlobalService(
typeof(SVsShellMonitorSelection));
monitorSelection.GetCurrentSelection(out hierarchyPointer,
out projectItemId,
out multiItemSelect,
out selectionContainerPointer);
IVsHierarchy selectedHierarchy = Marshal.GetTypedObjectForIUnknown(
hierarchyPointer,
typeof(IVsHierarchy)) as IVsHierarchy;
if (selectedHierarchy != null)
{
ErrorHandler.ThrowOnFailure(selectedHierarchy.GetProperty(
projectItemId,
(int)__VSHPROPID.VSHPROPID_ExtObject,
out selectedObject));
}
Project selectedProject = selectedObject as Project;
关于c# - 从解决方案资源管理器中选择的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11049758/