UI 自动化概述UI Automation Overview

备注

本文档适用于想要使用 UI 自动化UI Automation 命名空间中定义的托管 System.Windows.Automation 类的 .NET Framework 开发人员。This documentation is intended for .NET Framework developers who want to use the managed UI 自动化UI Automation classes defined in the System.Windows.Automation namespace. 有关 UI 自动化UI Automation的最新信息,请参阅 Windows 自动化 API:UI 自动化For the latest information about UI 自动化UI Automation, see Windows Automation API: UI Automation.

Microsoft UI 自动化Microsoft UI Automation是 Microsoft Windows 的新辅助功能框架,在所有支持的操作系统上都可用 Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF)is the new accessibility framework for Microsoft Windows, available on all operating systems that support Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF).

UI 自动化UI Automation 提供对桌面上大多数 用户界面 (UI)user interface (UI) 元素的编程访问,使辅助技术产品(如屏幕阅读器)能够使用标准输入以外的其他方式向最终用户提供有关 UIUI 的信息并操纵 UIUIprovides programmatic access to most 用户界面 (UI)user interface (UI) elements on the desktop, enabling assistive technology products such as screen readers to provide information about the UIUI to end users and to manipulate the UIUI by means other than standard input. UI 自动化UI Automation 也使自动化的测试脚本能够与 UIUI进行交互。also allows automated test scripts to interact with the UIUI.

备注

UI 自动化UI Automation 不会启用由不同用户通过“Run as” **** 命令启动的进程之间的通信。does not enable communication between processes started by different users through the Run as command.

UI 自动化客户端应用程序可在保证其将在多个框架上运行的情况下进行编写。UI Automation client applications can be written with the assurance that they will work on multiple frameworks. UI 自动化UI Automation 核心掩盖位于 UIUI各组成部分之下的框架中的任何差异。The UI 自动化UI Automation core masks any differences in the frameworks that underlie various pieces of UIUI. 例如,在 Content WPFWPF 视图中,按钮的属性、 Caption Win32 按钮的属性和 HTML 图像的属性都 ALT 映射到单个属性 Name UI 自动化UI AutomationFor example, the Content property of a WPFWPF button, the Caption property of a Win32 button, and the ALT property of an HTML image are all mapped to a single property, Name, in the UI 自动化UI Automation view.

UI 自动化在运行 .NET Framework 的受支持的 Windows 操作系统上提供了完整的功能(请参阅从 .NET Core 3.0 开始.NET Framework 系统要求或 .net core 版本。UI Automation provides full functionality on supported Windows operating systems running the .NET Framework (see .NET Framework system requirements or versions of .NET Core starting with .NET Core 3.0.

UI 自动化提供程序通过内置的桥接服务提供对 Microsoft Active Accessibility 客户端应用程序的一些支持。UI Automation providers offer some support for Microsoft Active Accessibility client applications through a built-in bridging service.

提供程序和客户端Providers and Clients

如下表所示,UI 自动化UI Automation 具有四个主要组件。UI 自动化UI Automation has four main components, as shown in the following table.

组件Component 说明Description
提供程序 API (UIAutomationProvider.dll 和 UIAutomationTypes.dll)Provider API (UIAutomationProvider.dll and UIAutomationTypes.dll) 一组接口定义,它们由 UI 自动化提供程序、提供关于 UIUI 元素信息并响应编程输入的对象来实现。A set of interface definitions that are implemented by UI Automation providers, objects that provide information about UIUI elements and respond to programmatic input.
客户端 API(UIAutomationClient.dll 和 UIAutomationTypes.dll)Client API (UIAutomationClient.dll and UIAutomationTypes.dll) 一组托管代码的类型,它们使 UI 自动化客户端应用程序可以获取有关 UIUI 的信息,并将输入发送到控件。A set of types for managed code that enables UI Automation client applications to obtain information about the UIUI and to send input to controls.
UiAutomationCore.dllUiAutomationCore.dll 用于处理提供程序和客户端之间通信的基础代码(有时称为 UI 自动化UI Automation 核心)。The underlying code (sometimes called the UI 自动化UI Automation core) that handles communication between providers and clients.
UIAutomationClientsideProviders.dllUIAutomationClientsideProviders.dll 一组用于标准旧版本控件的 UI 自动化提供程序。A set of UI Automation providers for standard legacy controls. WPFWPF 控件具有对的本机支持 UI 自动化UI Automation 。)此支持自动提供给客户端应用程序。(WPFWPF controls have native support for UI 自动化UI Automation.) This support is automatically available to client applications.

从软件开发人员的角度来看,有以下两种使用 UI 自动化UI Automation的方式:创建对自定义控件的支持(使用提供程序 API),创建使用 UI 自动化UI Automation 核心以与 UIUI 元素通信的应用程序(使用客户端 API)。From the software developer's perspective, there are two ways of using UI 自动化UI Automation: to create support for custom controls (using the provider API), and creating applications that use the UI 自动化UI Automation core to communicate with UIUI elements (using the client API). 具体取决于侧重点,你应参考文档中的其他部分。Depending on your focus, you should refer to different parts of the documentation. 你可在以下各节中进一步了解概念并获得实际操作知识。You can learn more about the concepts and gain practical how-to knowledge in the following sections.

部分Section 行业专家Subject matter 目标受众Audience
UI 自动化基础知识(本节)UI Automation Fundamentals (this section) 对概念的广泛概述。Broad overviews of the concepts. 全部。All.
托管代码的 UI 自动化提供程序UI Automation Providers for Managed Code 可帮助你使用提供程序 API 的概述和帮助主题。Overviews and how-to topics to help you use the provider API. 控件开发人员。Control developers.
托管代码的 UI 自动化客户端UI Automation Clients for Managed Code 可帮助你使用客户端 API 的概述和帮助主题。Overviews and how-to topics to help you use the client API. 客户端应用程序开发人员。Client application developers.
UI 自动化控件模式UI Automation Control Patterns 有关提供程序应如何实现控件模式以及哪些功能可供客户端使用的信息。Information about how control patterns should be implemented by providers, and what functionality is available to clients. 全部。All.
UI 自动化文本模式UI Automation Text Pattern 有关提供程序应如何实现 Text 控件模式以及哪些功能可供客户端使用的信息。Information about how the Text control pattern should be implemented by providers, and what functionality is available to clients. 全部。All.
UI 自动化控件类型UI Automation Control Types 有关不同控件类型支持的属性和控件模式的信息。Information about the properties and control patterns supported by different control types. 全部。All.

下表列出 UI 自动化UI Automation 命名空间、包含这些命名空间的 DLL 以及使用它们的受众。The following table lists UI 自动化UI Automation namespaces, the DLLs that contain them, and the audience that uses them.

命名空间Namespace 被引用的 DLLReferenced DLLs 目标受众Audience
System.Windows.Automation UIAutomationClientUIAutomationTypesUIAutomationClientUIAutomationTypes UI 自动化客户端开发人员;用于查找 AutomationElement 对象、注册 UI 自动化UI Automation 事件,并使用 UI 自动化UI Automation 控件模式。UI Automation client developers; used to find AutomationElement objects, register for UI 自动化UI Automation events, and work with UI 自动化UI Automation control patterns.
System.Windows.Automation.Provider UIAutomationProviderUIAutomationTypesUIAutomationProviderUIAutomationTypes WPFWPF以外的其他框架的 UI 自动化提供程序的开发人员。Developers of UI Automation providers for frameworks other than WPFWPF.
System.Windows.Automation.Text UIAutomationClientUIAutomationTypesUIAutomationClientUIAutomationTypes WPFWPF以外的其他框架的 UI 自动化提供程序的开发人员;用于实现 TextPattern 控件模式。Developers of UI Automation providers for frameworks other than WPFWPF; used to implement the TextPattern control pattern.
System.Windows.Automation.Peers PresentationFrameworkPresentationFramework WPFWPF的 UI 自动化提供程序的开发人员。Developers of UI Automation providers for WPFWPF.

UI 自动化模型UI Automation Model

UI 自动化UI Automation UIUI 的每个部分向客户端应用程序公开为 AutomationElementexposes every piece of the UIUI to client applications as an AutomationElement. 元素包含在树状结构,而桌面作为根元素。Elements are contained in a tree structure, with the desktop as the root element. 客户端可将树的原始视图筛选为控件视图或内容视图。Clients can filter the raw view of the tree as a control view or a content view. 应用程序还可以创建自定义视图。Applications can also create custom views.

AutomationElement 对象公开它们所代表的 UIUI 元素的公共属性。AutomationElement objects expose common properties of the UIUI elements they represent. 这些属性之一则是控件类型,它将其基本外观和功能定义为单个可识别的实体:如按钮或复选框。One of these properties is the control type, which defines its basic appearance and functionality as a single recognizable entity: for example, a button or check box.

此外,元素公开向其控件类型提供特定属性的控件模式。In addition, elements expose control patterns that provide properties specific to their control types. 控件模式还公开方法,使客户端获取有关该元素的详细信息,并提供输入。Control patterns also expose methods that enable clients to get further information about the element and to provide input.

备注

控件类型和控件模式之间并无一一对应关系。There is not a one-to-one correspondence between control types and control patterns. 一个控件模式可被多个控件类型支持,且一个控件可支持多个控件模式,两者均公开其行为的不同方面。A control pattern may be supported by multiple control types, and a control may support multiple control patterns, each of which exposes different aspects of its behavior. 例如,组合框中至少具有两个控件模式:一个表示可展开和折叠的功能,另一个代表选择机制。For example, a combo box has at least two control patterns: one that represents its ability to expand and collapse, and another that represents the selection mechanism. 有关详细信息,请参阅 UI Automation Control TypesFor specifics, see UI Automation Control Types.

UI 自动化UI Automation 同样通过事件向客户端应用程序提供信息。also provides information to client applications through events. 不同于了 winevent, UI 自动化UI Automation 事件不基于广播机制。Unlike WinEvents, UI 自动化UI Automation events are not based on a broadcast mechanism. UI 自动化UI Automation 客户端注册特定的事件通知且可以请求将特定 UI 自动化UI Automation 属性和控件模式信息传递给其事件处理程序。clients register for specific event notifications and can request that specific UI 自动化UI Automation properties and control pattern information be passed into their event handlers. 此外, UI 自动化UI Automation 事件包含对引发它的元素的引用。In addition, a UI 自动化UI Automation event contains a reference to the element that raised it. 提供程序可以通过有选择地引发事件来提高性能,具体取决于任一客户端是否正在进行侦听。Providers can improve performance by raising events selectively, depending on whether any clients are listening.

另请参阅See also