Share via


如何:使用 UIHierarchy 操作树视图

Visual Studio 2013 中已弃用 Visual Studio 的外接程序。 你应该升级外接程序到 VS 的扩展包。 有关升级的更多信息,请参见 。常见问题:将外接程序转换为 VSPackage 扩展

Visual Studio 中的某些工具窗口(如“Macro 资源管理器”和“解决方案资源管理器”)没有显式的自动化对象可用于操作其内容。 但是,这些工具窗口确实具有一个可以按编程方式访问的树视图,它是一个分层的大纲样式的节点视图。 UIHierarchy 对象表示这些工具窗口中的树视图,使您可以循环访问树视图和查看其节点的内容。

对象名

描述

UIHierarchy 对象

表示指定工具窗口中的树视图。

UIHierarchyItems 集合

表示树视图中的所有节点。

UIHierarchyItem 对象

表示树视图中的一个节点。

通过使用这些对象和集合,您可以:

  • 选择(一个或多个)并查看树视图中的节点。

  • 在树视图中上下移动插入点。

  • 返回选定项的值或使选定项执行其默认操作。

通过 ToolWindows 引入的 ToolWindows 对象(也是从 Visual Studio 返回的),可更轻松地引用中的各种工具窗口。 例如,现在您可以使用 _applicationObject.ToolWindows.OutputWindow,而不需要使用 _applicationObject.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)。

备注

显示的对话框和菜单命令可能会与“帮助”中的描述不同,具体取决于您现用的设置或版本。这些过程是在“常规开发设置”处于活动状态时开发的。若要更改设置,请在“工具”菜单上选择“导入和导出设置”。有关详细信息,请参阅在 Visual Studio 中自定义开发设置

示例

虽然 UIHierarchy 对象表示具有树视图的几乎任何工具窗口的内容,如“解决方案资源管理器”或[“Macro资源管理器”T:EnvDTE.Window,但是工具窗口本身仍为] 对象。 该 UIHierarchyItems 属性返回指定工具窗口中顶级节点的集合。 在**“解决方案资源管理器”**中,只有一个顶级节点,即解决方案。 因此,这些特定窗口的项目节点位于顶级节点的集合中,而不是位于窗口的 UIHierarchyItems 集合中。

请记住以上原则,可以使用两种方法来访问树视图中的某个特定节点 (UIHierarchyItem):

  • 通过使用 GetItem 方法可以直接引用使用解决方案/项目/项模式的所需节点。

  • 通过使用 UIHierarchyItems.Item.UIHierarchyItems...(集合/项/集合模式)。

    若要定位到更深的节点嵌套中,只需继续使用此模式即可。 例如,若要转到从属于顶级节点的节点,请使用 UIHierarchy.UIHierarchyItems.Item(1).UIHierarchyItems.Item(2)。

下面的示例演示如何使用两种技术来访问较低级别节点的示例。

这些外接程序示例演示如何引用和使用 UIHierarchy 自动化模型的各种成员列表中的所有 "解决方案资源管理器"的项目。

第一个示例使用 GetItem 方法策略访问**“解决方案资源管理器”**中的引用内容节点。 有关如何运行外接程序代码的更多信息,请参见 如何:编译和运行自动化对象模型代码示例

备注

"解决方案资源管理器" 的示例将其数据的消息框。

Imports System.Text

Public Sub OnConnection(ByVal application As Object, ByVal  _
connectMode As ext_ConnectMode, ByVal addInInst As Object,  _
ByRef custom As Array) Implements IDTExtensibility2.OnConnection
    _applicationObject = CType(application, DTE2)
    _addInInstance = CType(addInInst, AddIn)
    listSlnExpNodes(_applicationObject)
End Sub

Sub listSlnExpNodes(dte as DTE2)
    ' Requires reference to System.Text for StringBuilder.
    Dim UIH As UIHierarchy = dte.ToolWindows.SolutionExplorer
    ' Set a reference to the first level nodes in Solution Explorer. 
    ' Automation collections are one-based.
    Dim UIHItem As UIHierarchyItem = _
      UIH.GetItem("MyAddin1\MyAddin1\References")
    Dim file As UIHierarchyItem
    Dim sb As New StringBuilder

    ' Iterate through first level nodes.
    For Each file In UIHItem.UIHierarchyItems
        sb.AppendLine(file.Name)
        ' Iterate through second level nodes (if they exist).
        Dim subitem As UIHierarchyItem
        For Each subitem In file.UIHierarchyItems
            sb.AppendLine("   " & subitem.Name)
        Next
    Next
    MsgBox(sb.ToString)
End Sub
using System.Text;

public void OnConnection(object application, ext_ConnectMode _
  connectMode, object addInInst, ref Array custom)
{
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;
    listSlnExpNodes(_applicationObject);
}

public void listSlnExpNodes(DTE2 dte)
{
    // Requires reference to System.Text for StringBuilder.
    UIHierarchy UIH = dte.ToolWindows.SolutionExplorer;
    // Set a reference to the first level nodes in Solution Explorer. 
    // Automation collections are one-based.
    UIHierarchyItem UIHItem = 
      UIH.GetItem("MyAddin1\\MyAddin1\\References");
    StringBuilder sb = new StringBuilder();

   // Iterate through first level nodes.
   foreach ( UIHierarchyItem file in UIHItem.UIHierarchyItems )
   {
       sb.AppendLine(file.Name);
       // Iterate through second level nodes (if they exist).
       foreach ( UIHierarchyItem subitem in file.UIHierarchyItems )
       {
           sb.AppendLine("   "+subitem.Name);
       }
   }
   MessageBox.Show(sb.ToString());
}

下面的宏示例演示如何使用 UIHierarchy 列出“解决方案资源管理器”窗口的树视图的内容。

Sub cvTreeView()
    Dim uih As UIHierarchy = DTE.ToolWindows.SolutionExplorer
    Dim uihItem As UIHierarchyItem
    Dim uihItems As UIHierarchyItems = uih.UIHierarchyItems
    Dim msg As String
    For Each uihItem In uihItems
        msg += uihItem.Name & vbCr
    Next
    MsgBox(msg)
End Sub

请参见

任务

如何:控制解决方案资源管理器

如何:更改窗口特性

概念

自动化对象模型图表

其他资源

创建和控制环境窗口

创建外接程序和向导

自动化与扩展性参考