Ottenere pattern di controllo supportati per automazione interfaccia utente

Nota

Questa documentazione è destinata agli sviluppatori .NET Framework che desiderano utilizzare le classi di UI Automation gestite definite nello spazio dei nomi System.Windows.Automation. Per informazioni aggiornate su UI Automation, vedere API di automazione di Windows: UI Automation.

In questo argomento viene illustrato come recuperare oggetti del pattern di controllo dagli elementi di UI Automation.

Ottenere tutti i pattern di controllo

  1. Ottenere AutomationElement contenente i pattern di controllo interessati.

  2. Chiamare GetSupportedPatterns per ottenere tutti i pattern di controllo dall'elemento.

Attenzione

È consigliabile che un client non usi GetSupportedPatterns. Le prestazioni possono venire influenzate in modo significativo poiché questo metodo chiama GetCurrentPattern internamente per ogni pattern di controllo esistente. Se possibile, un client deve chiamare GetCurrentPattern per i principali pattern di interesse.

Ottenere un pattern di controllo specifico

  1. Ottenere AutomationElement contenente i pattern di controllo interessati.

  2. Chiamare GetCurrentPattern o TryGetCurrentPattern per eseguire query per un pattern specifico. Questi metodi sono simili, ma se il pattern non viene trovato, GetCurrentPattern genera un'eccezione e TryGetCurrentPattern restituisce false.

Esempio

Nell'esempio seguente viene recuperato un AutomationElement per una voce di elenco e viene ottenuto un SelectionItemPattern da tale elemento.

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

Vedi anche