共用方式為


如何:使用 UIHierarchy 管理樹狀檢視

.Visual Studio 增益集在 Visual Studio 2013 中已不適用。 您應該升級您的增益集至 VSPackage 擴充套件。 如需升級的詳細資訊,請參閱 常見問題集:將增益集轉換成 VSPackage 擴充功能

在 Visual Studio 中,有些工具視窗 (例如 [方案總管]) 並沒有明確的 Automation 物件可以用來管理其內容。 但是,這些工具視窗都有樹狀檢視 (也就是具有階層結構、大綱樣式的節點檢視),您可以用程式設計方式進行存取。 UIHierarchy 物件即表示這些工具視窗中的樹狀檢視,可以讓您逐一查看並檢視各節點的內容。

物件名稱

描述

UIHierarchy 物件

表示指定工具視窗中的樹狀檢視。

UIHierarchyItems 集合

表示樹狀檢視中的所有節點。

UIHierarchyItem 物件

表示樹狀檢視中的一個節點。

使用這些物件和集合,您可以:

  • 選取 (單一或多重) 並檢視樹狀檢視中的節點。

  • 在樹狀檢視中上下移動插入點。

  • 傳回選取項目的值,或者讓它執行預設動作。

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 Automation 模型的各個成員,以列出所有在 [方案總管] 的項目。

第一個範例會使用在 [方案總管] 中存取 [參考] 節點內容的 GetItem 方法策略。 如需如何執行增益集程式碼的詳細資訊,請參閱 如何:編譯和執行 Automation 物件模型程式碼範例

注意事項注意事項

[方案總管] 的範例將其資料傳送至訊息方塊。

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

請參閱

工作

如何:控制方案總管

如何:變更視窗特性

概念

Automation 物件模型圖表

其他資源

建立和控制環境視窗

建立增益集和精靈

Automation 與擴充性參考