注释
本文档适用于想要使用 System.Windows.Automation 命名空间中定义的托管 UI 自动化类的 .NET Framework 开发人员。 有关 UI 自动化的最新信息,请参阅 Windows 自动化 API:UI 自动化。
本概述介绍 UI 自动化客户端的控件模式。 它包含有关 UI 自动化客户端如何使用控件模式访问有关用户界面(UI)的信息。
控件模式提供了一种对控件的功能进行分类和公开的方法,与控件类型或控件的外观无关。 UI 自动化客户端可以检查 AutomationElement 并确定其支持哪些控件模式,从而保证该控件的行为。
有关控件模式的完整列表,请参阅 UI 自动化控件模式概述。
获取控件模式
客户通过调用AutomationElement或AutomationElement.GetCachedPattern从AutomationElement.GetCurrentPattern中检索控制模式。
客户端可以使用 GetSupportedPatterns 该方法或单个 IsPatternAvailable
属性(例如) IsTextPatternAvailableProperty来确定模式或模式组是否受 AutomationElement支持。 但是,尝试获取控件模式并测试 null
引用比检查支持的属性并检索控件模式更有效,因为它会导致更少的跨进程调用。
下面的示例演示如何从TextPattern获取AutomationElement控制模式。
// 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 外观。
实现自定义控件类型的控件可以具有表示其功能所需的任意控件模式集。