【问题标题】:Find out which reference is clicked in a Visual Studio extension找出在 Visual Studio 扩展中单击了哪个引用
【发布时间】:2016-07-13 08:02:07
【问题描述】:

我正在开发一个 Visual Studio 扩展,在其中我将元素添加到项目中引用的右键单击(上下文)菜单。这是通过使用 IDM_VS_CTXT_REFERENCE 的父级定义 Group 来完成的。

我想根据单击的引用显示-隐藏菜单元素,因此我将菜单项定义为OleMenuCommand

if (commandService != null)
{
    var menuCommandID = new CommandID(CommandSet, CommandId);
    var menuItem = new OleMenuCommand(this.MenuItemCallback, menuCommandID);

    menuItem.BeforeQueryStatus += (sender, args) =>
    {
        var button = (OleMenuCommand)sender;
        button.Visible = this.CommandVisible();
    };

    commandService.AddCommand(menuItem);
}

我无法实现CommandVisible 方法。假设为了示例的缘故,如果引用的名称以A 开头,我想显示菜单。我该怎么做?

我觉得我被困在互操作地狱中,盲目地绊倒在任意 id、guid 和不存在/难以理解的文档上。

我已经设法挖掘出我的参考作为IVsProject 和一些参考ID 的项目,但调用GetMkDocument 不会返回任何内容(它适用于项目中的文件,但不适用于参考)。

我该怎么做?我在哪里可以找到有关如何执行此操作的文档?

【问题讨论】:

  • GetMkDocument 仅对实际文档有效,参考只是视觉帮助,并非实际文件。我正在做一些测试,看看能不能帮到你。
  • 工作出现了,但我已经尽了最大的努力,关键是要使用 IVsHierarchy 方法和 itemid。我认为你在正确的轨道上。

标签: visual-studio visual-studio-extensions project-reference


【解决方案1】:

终于明白了。一旦您拥有所选项目的 IVsHierarchy 和 itemid,此行将在 out 参数中为您提供所需的名称。

hierarchy.GetProperty(itemid, (int)__VSHPROPID.VSHPROPID_Name, out name);

完整代码

object name;
uint  itemid = VSConstants.VSITEMID_NIL;
IVsMultiItemSelect multiItemSelect = null;
IntPtr hierarchyPtr = IntPtr.Zero;
IntPtr selectionContainerPtr = IntPtr.Zero;
try
{
    var monitorSelection = Package.GetGlobalService( typeof( SVsShellMonitorSelection ) ) as IVsMonitorSelection;
    monitorSelection.GetCurrentSelection( out hierarchyPtr, out itemid, out multiItemSelect, out selectionContainerPtr );
    hierarchy = Marshal.GetObjectForIUnknown( hierarchyPtr ) as IVsHierarchy;    
    hierarchy.GetProperty(itemid, (int)__VSHPROPID.VSHPROPID_Name, out name);
}finally
{
     if (selectionContainerPtr != IntPtr.Zero)
         Marshal.Release( selectionContainerPtr );

      if (hierarchyPtr != IntPtr.Zero)
          Marshal.Release( hierarchyPtr );
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多