客户端的 UI 自动化控件模式
更新:2007 年 11 月
本概述介绍 UI 自动化客户端的控件模式。它叙述了 UI 自动化客户端如何使用控件模式才能访问有关用户界面 (UI) 的信息。
控件模式提供一种将控件功能加以分类和公开的方式,这种方式与控件类型或控件外观无关。UI 自动化客户端可以检查 AutomationElement 以确定支持的控件模式和确保控件的行为。
有关控件模式的完整列表,请参见 UI 自动化控件模式概述。
本主题包括下列各节。
- 获取控件模式
- 检索控件模式中的属性
- 具有可变模式的控件
- 相关主题
获取控件模式
客户端通过调用 AutomationElement.GetCachedPattern 或 AutomationElement.GetCurrentPattern 从 AutomationElement 检索控件模式。
客户端可以使用 GetSupportedPatterns 方法或单个 IsPatternAvailable 属性(例如 IsTextPatternAvailableProperty)确定 AutomationElement 是否支持一个模式或一组模式。但是,尝试获取控件模式并测试 null 引用比检查受支持的属性并检索控件模式的效率更高,因为它产生的交叉进程调用更少。
下面的示例演示如何从 AutomationElement 获取 TextPattern 控件模式。
// Specify the control type we're looking for, in this case 'Document'
PropertyCondition cond = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document);
// target --> The root AutomationElement.
AutomationElement textProvider = target.FindFirst(TreeScope.Descendants, cond);
targetTextPattern = textProvider.GetCurrentPattern(TextPattern.Pattern) as TextPattern;
if (targetTextPattern == null)
{
Console.WriteLine("Root element does not contain a descendant that supports TextPattern.");
return;
}
检索控件模式中的属性
客户端可以通过调用 AutomationElement.GetCachedPropertyValue 或 AutomationElement.GetCurrentPropertyValue 并将返回的对象强制转换为相应类型来检索控件模式中的属性值。有关 UI 自动化属性的更多信息,请参见客户端的 UI 自动化属性。
除 GetPropertyValue 方法之外,还可以通过 公共语言运行时 (CLR) 访问器访问模式中的 UI 自动化属性来检索属性值。
具有可变模式的控件
某些控件类型支持不同的模式,这取决于控件的使用状态或方式。可具有可变模式的控件示例有:列表视图(缩略图、平铺、图标、列表和详细信息)Microsoft Excel 图表(饼图、折线图、柱状图和带有公式的单元格值)、Microsoft Word 的文档区域(普通、Web 版式、大纲、打印版式和打印预览)和 Microsoft Windows Media Player 外观。
控件实现自定义控件类型可以有任意一组表示其功能所需的控件模式。
请参见
任务
InvokePattern、ExpandCollapsePattern 和 TogglePattern 示例