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

Nota

Questa documentazione è destinata agli sviluppatori .NET Framework che desiderano utilizzare le classi di UI Automation gestite definite nello spazio dei nomi System.Windows.Automation. Per informazioni aggiornate su UI Automation, vedere API di automazione di Windows: UI Automation.

Questo argomento illustra come recuperare le proprietà di un elemento di UI Automation.

Ottenere il valore corrente di una proprietà

  1. Ottenere il AutomationElement di cui si desidera ottenere la proprietà.

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

Ottenere il valore di una proprietà memorizzato nella cache

  1. Ottenere il AutomationElement di cui si desidera ottenere la proprietà. La proprietà deve essere stata specificata in CacheRequest.

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

Esempio

Nell'esempio seguente vengono illustrati vari modi per recuperare le proprietà correnti di un 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

Vedi anche