共用方式為


取得 UI 自動化項目屬性

注意事項注意事項

這份文件適用於想要使用 System.Windows.Automation 命名空間中定義之 Managed UI Automation 類別的 .NET Framework 開發人員。如需 UI Automation 的最新資訊,請參閱 Windows Automation API:使用者介面自動化 (英文)。

本主題說明如何擷取 UI Automation項目的屬性。

取得目前屬性值

  1. 取得想要其屬性的 AutomationElement

  2. 呼叫 GetCurrentPropertyValue,或擷取 Current 屬性結構,然後從其中一個成員取得值。

取得快取屬性值

  1. 取得想要其屬性的 AutomationElement。 屬性必須已在 CacheRequest 中指定。

  2. 呼叫 GetCachedPropertyValue,或擷取 Cached 屬性結構,然後從其中一個成員取得值。

範例

下列範例會示範擷取 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;
}

請參閱

工作

使用 UI 自動化中的快取

概念

用戶端的 UI 自動化屬性

UI 自動化用戶端中的快取