AutomationElement.GetCurrentPattern(AutomationPattern) Método

Definição

Recupera o objeto padrão especificado neste AutomationElement.Retrieves the specified pattern object on this 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

O identificador do padrão a ser recuperado.The identifier of the pattern to retrieve.

Retornos

O objeto padrão, se o padrão especificado for compatível com o AutomationElement.The pattern object, if the specified pattern is currently supported by the AutomationElement.

Exceções

O padrão não é compatível com o elemento.The pattern is not supported by the element.

A interface do usuário do AutomationElement não existe mais.The UI for the AutomationElement no longer exists.

Exemplos

O exemplo a seguir mostra como usar esse método para recuperar um SelectionItemPattern, que é usado para selecionar um item em uma caixa de listagem.The following example shows how to use this method to retrieve a SelectionItemPattern, which is then used to select an item in a list box.

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

Observação

Para tarefas repetidas com frequência, como a do exemplo, seria mais eficiente armazenar em cache o padrão e usar GetCachedPattern.For often-repeated tasks such as the one in the example, it would be more efficient to cache the pattern and use GetCachedPattern.

Comentários

GetCurrentPatternObtém o padrão especificado com base em sua disponibilidade no momento da chamada.GetCurrentPattern gets the specified pattern based on its availability at the time of the call.

Para algumas formas do Interface de UsuárioUI, esse método incorrerá em sobrecarga de desempenho entre processos.For some forms of Interface de UsuárioUI, this method will incur cross-process performance overhead. Os aplicativos podem concentrar a sobrecarga armazenando padrões de cache e, em GetCachedPatternseguida, recuperando-os usando o.Applications can concentrate overhead by caching patterns and then retrieving them by using GetCachedPattern.

Aplica-se a

Veja também