Obter propriedades de elemento da automação de interface do usuário

Observação

Esta documentação destina-se a desenvolvedores de .NET Framework que querem usar as classes da Automação da Interface do Usuário gerenciadas definidas no namespace System.Windows.Automation. Para obter as informações mais recentes sobre a Automação da Interface do Usuário, confira API de Automação do Windows: Automação da Interface do Usuário.

Este tópico mostra como recuperar propriedades de um elemento de Automação da Interface do Usuário.

Obter um Valor da Propriedade Atual

  1. Obtenha o AutomationElement cuja propriedade você deseja conhecer.

  2. Chame GetCurrentPropertyValueou recupere a estrutura da propriedade Current e obtenha o valor de um dos seus membros.

Obter um Valor de Propriedade em Cache

  1. Obtenha o AutomationElement cuja propriedade você deseja conhecer. A propriedade deve ter sido especificada na CacheRequest.

  2. Chame GetCachedPropertyValueou recupere a estrutura da propriedade Cached e obtenha o valor de um dos seus membros.

Exemplo

O exemplo a seguir mostra várias maneiras de recuperar as propriedades atuais de um AutomationElement.

void PropertyCallsExample(AutomationElement elementList)
{
    // The following two calls are equivalent.
    string name = elementList.Current.Name;
    name = elementList.GetCurrentPropertyValue(AutomationElement.NameProperty) as string;

    // The following shows how to ignore the default property, which
    //  would probably be an empty string if the property is not supported.
    //  Passing "false" as the second parameter is equivalent to using the overload
    //  that does not have this parameter.
    object help = elementList.GetCurrentPropertyValue(AutomationElement.HelpTextProperty, true);
    if (help == AutomationElement.NotSupported)
    {
        help = "No help available";
    }
    string helpText = (string)help;
}
Sub PropertyCallsExample(ByVal elementList As AutomationElement)
    ' The following two calls are equivalent.
    Dim name As String = elementList.Current.Name
    name = CStr(elementList.GetCurrentPropertyValue(AutomationElement.NameProperty))

    ' The following shows how to ignore the default property, which 
    '  would probably be an empty string if the property is not supported.
    '  Passing "false" as the second parameter is equivalent to using the overload
    '  that does not have this parameter.
    Dim help As Object = elementList.GetCurrentPropertyValue(AutomationElement.HelpTextProperty, True)
    If help Is AutomationElement.NotSupported Then
        help = "No help available"
    End If
    Dim helpText As String = CStr(help)

End Sub

Confira também