共用方式為


取得 UI 自動化項目屬性

注意

本文件適用對象為 .NET Framework 開發人員,其想要使用 System.Windows.Automation 命名空間中定義的受控 UI 自動化類別。 如需 UI 自動化的最新資訊,請參閱 Windows 自動化 API:UI 自動化

本主題說明如何擷取 UI 自動化元素的屬性。

取得目前的屬性值

  1. 取得您想要取得其屬性的 AutomationElement

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

取得快取屬性值

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

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

範例

下列範例說明擷取 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

另請參閱