用戶端的 UI 自動化控制項模式UI Automation Control Patterns for Clients

注意

這份文件適用於想要使用 UI 自動化UI Automation 命名空間中定義之 Managed 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 Automation API:UI 自動化For the latest information about UI 自動化UI Automation, see Windows Automation API: UI Automation.

本概觀介紹使用者介面自動化用戶端的控制項模式。This overview introduces control patterns for UI Automation clients. 其中所包含的資訊,與使用者介面自動化用戶端使用控制項模式來存取 使用者介面 (UI)user interface (UI)相關資訊之方式有關。It includes information on how a UI Automation client can use control patterns to access information about the 使用者介面 (UI)user interface (UI).

控制項模式提供一種方式,分類及公開與控制項類型或控制項外觀無關的控制項功能。Control patterns provide a way to categorize and expose a control's functionality independent of the control type or the appearance of the control. 使用者介面自動化用戶端可檢查 AutomationElement 來判斷支援哪些控制項模式,及保證控制項的行為。UI Automation clients can examine an AutomationElement to determine which control patterns are supported and be assured of the behavior of the control.

如需控制項模式的完整清單,請參閱 UI Automation Control Patterns OverviewFor a complete list of control patterns, see UI Automation Control Patterns Overview.

取得控制項模式Getting Control Patterns

用戶端藉由呼叫 AutomationElementAutomationElement.GetCachedPattern ,從 AutomationElement.GetCurrentPattern擷取控制項模式。Clients retrieve a control pattern from an AutomationElement by calling either AutomationElement.GetCachedPattern or AutomationElement.GetCurrentPattern.

用戶端可以使用 GetSupportedPatterns 方法或個別的 IsPatternAvailable 屬性 (例如, IsTextPatternAvailableProperty) 來判斷 AutomationElement上是否支援模式或模式群組。Clients can use the GetSupportedPatterns method or an individual IsPatternAvailable property (for example, IsTextPatternAvailableProperty) to determine if a pattern or group of patterns is supported on the AutomationElement. 不過,嘗試取得控制項模式和針對 null 參考進行測試,比檢查支援的屬性並擷取控制項模式更有效率,因為它會造成較少的跨處理序呼叫。However, it is more efficient to attempt to get the control pattern and test for a null reference than to check the supported properties and retrieve the control pattern since it results in fewer cross-process calls.

下列範例示範如何從 TextPattern 取得 AutomationElement控制項模式。The following example demonstrates how to get a TextPattern control pattern from an 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;
}

在控制項模式上擷取屬性Retrieving Properties on Control Patterns

用戶端可藉由呼叫 AutomationElement.GetCachedPropertyValueAutomationElement.GetCurrentPropertyValue ,並將物件轉型成傳回適當的類型,藉此在控制項模式上擷取屬性值。Clients can retrieve the property values on control patterns by calling either AutomationElement.GetCachedPropertyValue or AutomationElement.GetCurrentPropertyValue and casting the object returned to an appropriate type. 如需 UI 自動化UI Automation 屬性的詳細資訊,請參閱用戶端的 UI 自動化屬性For more information on UI 自動化UI Automation properties, see UI Automation Properties for Clients.

除了 GetPropertyValue 方法以外,您還可以透過 common language runtime (CLR)存取子來抓取屬性值,以存取模式的 UI 自動化UI Automation 屬性。In addition to the GetPropertyValue methods, property values can be retrieved through the common language runtime (CLR) accessors to access the UI 自動化UI Automation properties on a pattern.

包含變數模式的控制項Controls with Variable Patterns

某些控制項類型支援不同的模式,視其狀態或使用控制項的方式而定。Some control types support different patterns depending on their state or the manner in which the control is being used. 可以有變數模式的控制項範例包括清單視圖(縮圖、磚、圖示、清單、詳細資料)、Microsoft Excel 圖表(圓形圖、折線圖、橫條圖、具有公式的資料格值)、Microsoft Word 的檔區域(一般、Web 版面配置、大綱、列印版面配置、列印預覽)和 Microsoft Windows 媒體播放機的外觀。Examples of controls that can have variable patterns are list views (thumbnails, tiles, icons, list, details), Microsoft Excel Charts (Pie, Line, Bar, Cell Value with a formula), Microsoft Word's document area (Normal, Web Layout, Outline, Print Layout, Print Preview), and Microsoft Windows Media Player skins.

實作自訂控制項類型的控制項可以有任何控制項模式組合,它們是代表其功能所需的組合。Controls implementing custom control types can have any set of control patterns that are needed to represent their functionality.

請參閱See also