如何:使用 UIHierarchy 操作树视图
在 Visual Studio的某些工具窗口,例如 解决方案资源管理器,没有可用于操作其内容的显式自动化对象。 但是,这些工具窗口确实具有一个可以按编程方式访问的树视图,它是一个分层的大纲样式的节点视图。 UIHierarchy 对象表示这些工具窗口中的树视图,使您可以循环访问树视图和查看其节点的内容。
对象名 |
描述 |
---|---|
UIHierarchy 对象 |
表示指定工具窗口中的树视图。 |
表示树视图中的所有节点。 |
|
表示树视图中的一个节点。 |
通过使用这些对象和集合,您可以:
选择(一个或多个)并查看树视图中的节点。
在树视图中上下移动插入点。
返回选定项的值或使选定项执行其默认操作。
从 ToolWindows还返回) 的 ToolWindows 对象 (可更轻松地引用在 Visual Studio的各种工具窗口。 例如,现在您可以使用 _applicationObject.ToolWindows.OutputWindow,而不需要使用 _applicationObject.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)。
备注
显示的对话框和菜单命令可能会与“帮助”中的描述不同,具体取决于您现用的设置或版本。这些过程是在“常规开发设置”处于活动状态时开发的。若要更改设置,请在“工具”菜单上选择“导入和导出设置”。有关更多信息,请参见Visual Studio 设置。
示例
虽然 UIHierarchy 对象表示内容关闭具有树视图,例如 解决方案资源管理器,工具窗口中的所有工具窗口仍是 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