Ottenere le proprietà degli elementi di automazione dell'interfaccia utente

Aggiornamento: novembre 2007

In questo argomento viene illustrato come recuperare proprietà di un elemento automazione interfaccia utente.

Ottenere un valore di proprietà corrente

  1. Ottenere l'oggetto AutomationElement di cui si desidera ottenere la proprietà.

  2. Chiamare il metodo GetCurrentPropertyValue o recuperare la struttura della proprietà Current e ottenere il valore da uno dei membri.

Ottenere un valore di proprietà memorizzato nella cache

  1. Ottenere l'oggetto AutomationElement di cui si desidera ottenere la proprietà. È necessario che la proprietà sia stata specificata nell'oggetto CacheRequest.

  2. Chiamare il metodo GetCachedPropertyValue o recuperare la struttura della proprietà Cached e ottenere il valore da uno dei membri.

Esempio

Nell'esempio riportato di seguito vengono illustrate varie modalità per recuperare le proprietà correnti di un oggetto AutomationElement.

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 'PropertyCallsExample
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;
}

Vedere anche

Attività

Utilizzare la memorizzazione nella cache per l'automazione interfaccia utente

Concetti

Proprietà di automazione interfaccia utente per i client

Memorizzazione nella cache dei client di automazione interfaccia utente