获取 UI 自动化元素的属性

更新:2007 年 11 月

本主题演示如何检索 UI 自动化元素的属性。

获取当前的属性值

  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 自动化客户端中缓存