获取 UI 自动化元素的属性
备注
本文档适用于想要使用 System.Windows.Automation 命名空间中定义的托管 UI 自动化类的 .NET Framework 开发人员。 有关 UI 自动化的最新信息,请参阅 Windows 自动化 API:UI 自动化。
本主题演示如何检索 UI 自动化元素的属性。
获取当前属性值
获取要得到其属性的 AutomationElement。
调用 GetCurrentPropertyValue 或检索 Current 属性结构,并从其成员之一获取值。
获取缓存的属性值
获取要得到其属性的 AutomationElement。 必须已在 CacheRequest 中指定了属性。
调用 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