共用方式為


用戶端的 UI 自動化控制項模式

注意事項注意事項

這份文件適用於想要使用 System.Windows.Automation 命名空間中定義之 Managed UI Automation 類別的 .NET Framework 開發人員。如需 UI Automation 的最新資訊,請參閱 Windows Automation API:使用者介面自動化 (英文)。

本概觀簡介 UI 自動化用戶端的控制項模式。 其中說明 UI 自動化用戶端如何使用控制項模式,存取關於user interface (UI) 的資訊。

控制項模式可讓您分類及公開控制項的功能,而不受控制項型別或外觀所影響。 UI 自動化用戶端可以透過檢查 AutomationElement,判斷支援的控制項模式為何,並確保控制項的行為。

如需控制項模式的完整清單,請參閱 UI 自動化控制項模式概觀

這個主題包含下列章節。

  • 取得控制項模式
  • 擷取控制項模式的屬性
  • 有不同模式的控制項
  • 相關主題

取得控制項模式

用戶端可呼叫 AutomationElement.GetCachedPatternAutomationElement.GetCurrentPattern,從 AutomationElement 擷取控制項模式。

用戶端可以使用 GetSupportedPatterns 方法或個別的 IsPatternAvailable 屬性 (例如,IsTextPatternAvailableProperty),判斷 AutomationElement 是否支援某個模式或一組模式。 不過,試著取得控制項模式並測試是否為 null 的方式,要比檢查支援的屬性並擷取控制項模式更有效率,因為前者會產生較少的跨處理序 (Cross-Process) 呼叫。

在下列範例中,會示範如何從 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.GetCachedPropertyValueAutomationElement.GetCurrentPropertyValue 並將傳回的物件轉換為適當的型別,以擷取控制項模式的屬性值。 如需 UI Automation屬性的詳細資訊,請參閱用戶端的 UI 自動化屬性

除了 GetPropertyValue 方法之外,也可以透過 common language runtime (CLR) 存取子存取模式上的 UI Automation屬性,以擷取屬性值。

有不同模式的控制項

部分控制項型別會根據狀態或控制項的使用方式,支援不同的模式。 有不同模式的控制項包括清單檢視 (縮圖、並排、圖示、清單、詳細資料)、Microsoft Excel 圖表 (圓形圖、折線圖、橫條圖、含公式的儲存格值)、Microsoft Word 的文件區域 (標準、Web 版面配置、大綱、整頁模式、預覽列印) 以及 Microsoft Windows Media Player 面板。

實作自訂控制項型別的控制項可有執行其功能所需的任何一組控制項模式。

請參閱

工作

使用 UI 自動化叫用控制項

使用 UI 自動化取得核取方塊的切換狀態

ValuePattern Insert Text Sample

TextPattern Search and Selection Sample

InvokePattern, ExpandCollapsePattern, and TogglePattern Sample

概念

UI 自動化用戶端的控制項模式對應

其他資源

UI 自動化控制項模式

UI 自動化的文字模式