Delen via


Ondersteunde patronen voor ui-automatiseringsbeheer ophalen

Notitie

Deze documentatie is bedoeld voor .NET Framework-ontwikkelaars die de beheerde UI Automation-klassen willen gebruiken die zijn gedefinieerd in de System.Windows.Automation naamruimte. Zie Windows Automation-API: UI Automation voor de meest recente informatie over UI Automation.

In dit onderwerp wordt beschreven hoe u besturingspatroonobjecten ophaalt uit UI Automation-elementen.

Alle besturingspatronen verkrijgen

  1. Haal de AutomationElement controlepatronen op waarin u geïnteresseerd bent.

  2. Roep GetSupportedPatterns aan om alle controlepatronen van het element op te halen.

Let op

Het wordt sterk aanbevolen dat een client geen gebruik maakt GetSupportedPatternsvan . De prestaties kunnen ernstig worden beïnvloed omdat deze methode intern aanroept GetCurrentPattern voor elk bestaand besturingspatroon. Indien mogelijk moet een client de belangrijkste interessepatronen aanroepen GetCurrentPattern .

Een specifiek besturingselementpatroon verkrijgen

  1. Haal de AutomationElement controlepatronen op waarin u geïnteresseerd bent.

  2. Aanroepen GetCurrentPattern of TryGetCurrentPattern query's uitvoeren op een specifiek patroon. Deze methoden zijn vergelijkbaar, maar als het patroon niet wordt gevonden, GetCurrentPattern genereert u een uitzondering en TryGetCurrentPattern retourneert falseu .

Opmerking

In het volgende voorbeeld wordt een AutomationElement voor een lijstitem opgehaald en wordt een SelectionItemPattern van dat element opgehaald.

/// <summary>
/// Sets the focus to a list and selects a string item in that list.
/// </summary>
/// <param name="listElement">The list element.</param>
/// <param name="itemText">The text to select.</param>
/// <remarks>
/// This deselects any currently selected items. To add the item to the current selection
/// in a multiselect list, use AddToSelection instead of Select.
/// </remarks>
public void SelectListItem(AutomationElement listElement, String itemText)
{
    if ((listElement == null) || (itemText == ""))
    {
        throw new ArgumentException("Argument cannot be null or empty.");
    }
    listElement.SetFocus();
    Condition cond = new PropertyCondition(
        AutomationElement.NameProperty, itemText, PropertyConditionFlags.IgnoreCase);
    AutomationElement elementItem = listElement.FindFirst(TreeScope.Children, cond);
    if (elementItem != null)
    {
        SelectionItemPattern pattern;
        try
        {
            pattern = elementItem.GetCurrentPattern(SelectionItemPattern.Pattern) as SelectionItemPattern;
        }
        catch (InvalidOperationException ex)
        {
            Console.WriteLine(ex.Message);  // Most likely "Pattern not supported."
            return;
        }
        pattern.Select();
    }
}
''' <summary>
''' Sets the focus to a list and selects a string item in that list.
''' </summary>
''' <param name="listElement">The list element.</param>
''' <param name="itemText">The text to select.</param>
''' <remarks>
''' This deselects any currently selected items. To add the item to the current selection 
''' in a multiselect list, use AddToSelection instead of Select.
''' </remarks>
Public Sub SelectListItem(ByVal listElement As AutomationElement, ByVal itemText As String)
    If listElement Is Nothing OrElse itemText = "" Then
        Throw New ArgumentException("Argument cannot be null or empty.")
    End If
    listElement.SetFocus()
    Dim cond As New PropertyCondition(AutomationElement.NameProperty, itemText, PropertyConditionFlags.IgnoreCase)
    Dim elementItem As AutomationElement = listElement.FindFirst(TreeScope.Children, cond)
    If Not (elementItem Is Nothing) Then
        Dim pattern As SelectionItemPattern
        Try
            pattern = DirectCast(elementItem.GetCurrentPattern(SelectionItemPattern.Pattern), _
                SelectionItemPattern)
        Catch ex As InvalidOperationException
            Console.WriteLine(ex.Message) ' Most likely "Pattern not supported."
            Return
        End Try
        pattern.Select()
    End If

End Sub

Zie ook