Obtener patrones de control de UI Automation admitidos

Nota

Esta documentación está dirigida a los desarrolladores de .NET Framework que quieran usar las clases de automatización de la interfaz de usuario administradas definidas en el espacio de nombres System.Windows.Automation. Para ver la información más reciente acerca de la automatización de la interfaz de usuario, consulte Windows Automation API: automatización de la interfaz de usuario.

En este tema muestra cómo recuperar los objetos de patrón de control de elementos de automatización de la interfaz de usuario.

Obtener todos los patrones de control

  1. Obtenga el elemento AutomationElement que tenga los patrones en los que está interesado.

  2. Llame a GetSupportedPatterns para obtener todos los patrones de control del elemento.

Precaución

Se recomienda encarecidamente que un cliente no utilice GetSupportedPatterns. El rendimiento puede verse afectado gravemente a medida que este método llama a GetCurrentPattern internamente para cada patrón de control existente. Si es posible, un cliente debe llamar a GetCurrentPattern para los patrones clave de interés.

Obtener un patrón de Control concreto

  1. Obtenga el elemento AutomationElement que tenga los patrones en los que está interesado.

  2. Llame a GetCurrentPattern o TryGetCurrentPattern para consultar un patrón concreto. Estos métodos son similares, pero si no se encuentra el patrón, GetCurrentPattern genera una excepción y TryGetCurrentPattern devuelve false.

Ejemplo

En el ejemplo siguiente se recupera un valor AutomationElement para un elemento de lista y se obtiene un valor SelectionItemPattern de ese 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

Consulte también