TreeWalker(Condition) Конструктор

Определение

Инициализирует новый экземпляр класса TreeWalker.

public:
 TreeWalker(System::Windows::Automation::Condition ^ condition);
public TreeWalker (System.Windows.Automation.Condition condition);
new System.Windows.Automation.TreeWalker : System.Windows.Automation.Condition -> System.Windows.Automation.TreeWalker
Public Sub New (condition As Condition)

Параметры

condition
Condition

Представление дерева элементов модель автоматизации пользовательского интерфейса, которое TreeWalker будет перемещаться.

Примеры

В следующем примере показано, как создать навигацию TreeWalker только между включенными элементами.

/// <summary>
/// Walks the UI Automation tree and adds the control type of each enabled control 
/// element it finds to a TreeView.
/// </summary>
/// <param name="rootElement">The root of the search on this iteration.</param>
/// <param name="treeNode">The node in the TreeView for this iteration.</param>
/// <remarks>
/// This is a recursive function that maps out the structure of the subtree beginning at the
/// UI Automation element passed in as rootElement on the first call. This could be, for example,
/// an application window.
/// CAUTION: Do not pass in AutomationElement.RootElement. Attempting to map out the entire subtree of
/// the desktop could take a very long time and even lead to a stack overflow.
/// </remarks>
private void WalkEnabledElements(AutomationElement rootElement, TreeNode treeNode)
{
    Condition condition1 = new PropertyCondition(AutomationElement.IsControlElementProperty, true);
    Condition condition2 = new PropertyCondition(AutomationElement.IsEnabledProperty, true);
    TreeWalker walker = new TreeWalker(new AndCondition(condition1, condition2));
    AutomationElement elementNode = walker.GetFirstChild(rootElement);
    while (elementNode != null)
    {
        TreeNode childTreeNode = treeNode.Nodes.Add(elementNode.Current.ControlType.LocalizedControlType);
        WalkEnabledElements(elementNode, childTreeNode);
        elementNode = walker.GetNextSibling(elementNode);
    }
}
''' <summary>
''' Walks the UI Automation tree and adds the control type of each enabled control 
''' element it finds to a TreeView.
''' </summary>
''' <param name="rootElement">The root of the search on this iteration.</param>
''' <param name="treeNode">The node in the TreeView for this iteration.</param>
''' <remarks>
''' This is a recursive function that maps out the structure of the subtree beginning at the
''' UI Automation element passed in as rootElement on the first call. This could be, for example,
''' an application window.
''' CAUTION: Do not pass in AutomationElement.RootElement. Attempting to map out the entire subtree of
''' the desktop could take a very long time and even lead to a stack overflow.
''' </remarks>
Private Sub WalkEnabledElements(ByVal rootElement As AutomationElement, ByVal treeNode As TreeNode)
    Dim condition1 As New PropertyCondition(AutomationElement.IsControlElementProperty, True)
    Dim condition2 As New PropertyCondition(AutomationElement.IsEnabledProperty, True)
    Dim walker As New TreeWalker(New AndCondition(condition1, condition2))
    Dim elementNode As AutomationElement = walker.GetFirstChild(rootElement)
    While (elementNode IsNot Nothing)
        Dim childTreeNode As TreeNode = treeNode.Nodes.Add(elementNode.Current.ControlType.LocalizedControlType)
        WalkEnabledElements(elementNode, childTreeNode)
        elementNode = walker.GetNextSibling(elementNode)
    End While

End Sub

Комментарии

модель автоматизации пользовательского интерфейса элементы, которые не соответствуют, condition пропускаются при TreeWalker использовании для перемещения по дереву элементов.

Если клиентское приложение может попытаться найти элементы в собственном пользовательском интерфейсе, необходимо выполнить все вызовы модель автоматизации пользовательского интерфейса в отдельном потоке.

Применяется к

См. также раздел