AutomationElement.GetCurrentPattern(AutomationPattern) Método

Definición

Recupera el objeto del patrón especificado en AutomationElement.

public:
 System::Object ^ GetCurrentPattern(System::Windows::Automation::AutomationPattern ^ pattern);
public object GetCurrentPattern (System.Windows.Automation.AutomationPattern pattern);
member this.GetCurrentPattern : System.Windows.Automation.AutomationPattern -> obj
Public Function GetCurrentPattern (pattern As AutomationPattern) As Object

Parámetros

pattern
AutomationPattern

Identificador del patrón que se debe recuperar.

Devoluciones

Objeto del patrón, si AutomationElement admite actualmente el patrón especificado.

Excepciones

El elemento no admite el patrón.

La interfaz de usuario para el AutomationElement ya no existe.

Ejemplos

En el ejemplo siguiente se muestra cómo usar este método para recuperar un SelectionItemPattern, que luego se usa para seleccionar un elemento en un cuadro de lista.

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

Nota

Para tareas repetidas a menudo como la del ejemplo, sería más eficaz almacenar en caché el patrón y usar GetCachedPattern.

Comentarios

GetCurrentPattern obtiene el patrón especificado en función de su disponibilidad en el momento de la llamada.

Para algunas formas de interfaz de usuario, este método incurrirá en sobrecarga de rendimiento entre procesos. Las aplicaciones pueden concentrar la sobrecarga mediante el almacenamiento en caché de patrones y, a continuación, recuperarlos mediante GetCachedPattern.

Se aplica a

Consulte también