TreeWalker(Condition) Costruttore

Definizione

Inizializza una nuova istanza della classe 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)

Parametri

condition
Condition

Visualizzazione dell'albero degli elementi Automazione interfaccia utente che TreeWalker verrà spostato.

Esempio

Nell'esempio seguente viene illustrato come creare un oggetto TreeWalker che si sposta solo tra gli elementi abilitati.

/// <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

Commenti

Automazione interfaccia utente elementi che non corrispondono condition vengono ignorati quando TreeWalker viene usato per esplorare l'albero degli elementi.

Se l'applicazione client potrebbe provare a trovare elementi nella propria interfaccia utente, è necessario eseguire tutte le chiamate Automazione interfaccia utente in un thread separato.

Si applica a

Vedi anche