frameworke3.0 及以上html
using System.Windows.Automation;app
UIAutomationClient.dllide
UIAutomationClientsideProviders.dll C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\UIAutomationClientsideProviders.dll測試
UIAutomationProvider.dllui
UIAutomationTypes.dllthis
下面的示例演示如何使用 FindAll 查找窗口中的全部已啓用的按鈕。spa
/// <summary> /// Finds all enabled buttons in the specified window element. /// </summary> /// <param name="elementWindowElement">An application or dialog window.</param> /// <returns>A collection of elements that meet the conditions.</returns> AutomationElementCollection FindByMultipleConditions(AutomationElement elementWindowElement) { if (elementWindowElement == null) { throw new ArgumentException(); } Condition conditions = new AndCondition( new PropertyCondition(AutomationElement.IsEnabledProperty, true), new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button) ); // Find all children that match the specified conditions. AutomationElementCollection elementCollection = elementWindowElement.FindAll(TreeScope.Children, conditions); return elementCollection; }
本主題描述獲取 用戶界面 (UI) 元素的 AutomationElement 對象的各類方法。線程
![]() |
---|
若是客戶端應用程序能夠嘗試在其本身的用戶界面中查找元素,則必須在一個單獨的線程上進行全部 UI 自動化調用。有關更多信息,請參見 UI 自動化線程處理問題。code |
本主題包括下列各節。orm
全部 AutomationElement 對象搜索必須具備一個起點。此起點能夠是任何元素,包括桌面、應用程序窗口或控件。
全部元素所起源於的根元素是從靜態的 AutomationElement..::.RootElement 屬性中得到的。
![]() |
---|
一般,您應當嘗試只獲取 RootElement 的直接子項。對子代的搜索可能循環訪問數百個甚至數千個元素,從而可能致使堆棧溢出。若是嘗試在較低級別獲取特定元素,您應當從應用程序窗口或位於較低級別的容器中開始搜索。 |
對於用於檢索 UI 自動化元素的大多數方法,您必須指定 Condition,這是一組用於定義要檢索的元素的條件。
最簡單的條件是 TrueCondition,這是一個指定要返回搜索範圍內的全部元素的預約義對象。FalseCondition 是 TrueCondition 的對立條件,其做用不大,由於它將阻止找到任何元素。
下面是三個其餘的預約義條件,這些條件既能夠單獨使用,也能夠與其餘條件一塊兒使用:ContentViewCondition、ControlViewCondition 和 RawViewCondition。單獨使用的 RawViewCondition 等效於 TrueCondition,由於它不根據元素的 IsControlElement 或 IsContentElement 屬性來篩選元素。
其餘條件是根據一個或多個 PropertyCondition 對象(每一個對象都指定一個屬性值)創建的。例如,PropertyCondition 能夠指定元素處於啓用狀態或元素支持某種控件模式。
經過構建 AndCondition、OrCondition 和 NotCondition 類型的對象,能夠用布爾邏輯將條件結合起來。
用 FindFirst 或 FindAll 執行的搜索必須具備一個範圍和一個起點。
範圍定義了要搜索的起點周圍的空間。這能夠包括元素自己、其同級項、父項、祖先、直接子項以及子代。
搜索範圍由按位組合的 TreeScope 枚舉值來定義。
若要查找由已知元素的 Name、AutomationId、某些其餘屬性或屬性組合標識的已知元素,最簡單的方法是使用 FindFirst 方法。若是查找的元素是一個應用程序窗口,則搜索的起點能夠是 RootElement。
這種查找 UI 自動化 元素的方法在自動化測試方案中最有用。
若要查找與已知元素有關且知足特定條件的全部元素,您可使用 FindAll。例如,您可使用這種方法從列表或菜單中檢索列表項或菜單項,或找出對話框中的全部控件。
若是您事先不瞭解可與客戶端一塊兒使用的應用程序,則可使用 TreeWalker 類創建一個包含感興趣的全部元素的子樹。您的應用程序可能會執行此操做以響應 focus-changed 事件;也就是說,在應用程序或控件接收輸入焦點時,UI 自動化客戶端將檢查子項,可能還會檢查有焦點的元素的全部子代。
使用 TreeWalker 的另外一種方式是識別元素的祖先。例如,經過向上瀏覽樹,您能夠識別控件的父窗口。
經過建立類的對象(用 Condition 定義感興趣的元素),或者使用如下被定義爲 TreeWalker 的字段的預約義對象之一,您可使用 TreeWalker。
ContentViewWalker |
只查找 IsContentElement 屬性爲 true 的元素。 |
ControlViewWalker |
只查找 IsControlElement 屬性爲 true 的元素。 |
RawViewWalker |
查找全部元素。 |
在得到了 TreeWalker 以後,能夠直接使用它。只需調用 Get 方法即可在子樹的元素中導航。
Normalize 方法可用於從視圖外的另外一元素導航到子樹中的某個元素。例如,假設您使用 ContentViewWalker 建立了一個子樹視圖。而後,您的應用程序收到通知,得知滾動條已經接收了輸入焦點。由於滾動條不是內容元素,因此子樹視圖中未呈現滾動條。可是,您能夠將表明滾動條的 AutomationElement 傳遞到 Normalize 並檢索內容視圖中最接近的祖先。
除了應用搜索和導航以外,您還能夠經過如下方法來檢索 AutomationElement。
當您的應用程序接收 UI 自動化事件時,傳遞到事件處理程序的源對象爲 AutomationElement。例如,若是您訂閱了 focus-changed 事件,則傳遞到 AutomationFocusChangedEventHandler 的源是收到焦點的元素。
有關更多信息,請參見訂閱 UI 自動化事件。
若是您具備屏幕座標(例如,光標位置),則可使用靜態的 FromPoint 方法來檢索 AutomationElement。
若要從 HWND 中檢索 AutomationElement,請使用靜態的 FromHandle 方法。
能夠從靜態的 FocusedElement 屬性中檢索表示焦點控件的 AutomationElement。
表示一個 Condition,其計算結果老是爲 true。
命名空間: System.Windows.Automation
程序集: UIAutomationClient(在 UIAutomationClient.dll 中)
Visual Basic(聲明) |
---|
Public Shared ReadOnly TrueCondition As Condition |
Visual Basic(用法) |
---|
Dim value As Condition value = Condition.TrueCondition |
C# |
---|
public static readonly Condition TrueCondition |
Visual C++ |
---|
public: static initonly Condition^ TrueCondition |
J# |
---|
public static final Condition TrueCondition |
JScript |
---|
public static final var TrueCondition : Condition |
在下面的示例中,使用 TrueCondition 檢索在指定範圍內的所有 UI 自動化元素。
C# | ![]() |
---|---|
/// <summary> /// Examples of using predefined conditions to find elements. /// </summary> /// <param name="elementMainWindow">The element for the target window.</param> public void StaticConditionExamples(AutomationElement elementMainWindow) { if (elementMainWindow == null) { throw new ArgumentException(); } // Use TrueCondition to retrieve all elements. AutomationElementCollection elementCollectionAll = elementMainWindow.FindAll( TreeScope.Subtree, Condition.TrueCondition); Console.WriteLine("\nAll control types:"); foreach (AutomationElement autoElement in elementCollectionAll) { Console.WriteLine(autoElement.Current.Name); } // Use ContentViewCondition to retrieve all content elements. AutomationElementCollection elementCollectionContent = elementMainWindow.FindAll( TreeScope.Subtree, Automation.ContentViewCondition); Console.WriteLine("\nAll content elements:"); foreach (AutomationElement autoElement in elementCollectionContent) { Console.WriteLine(autoElement.Current.Name); } // Use ControlViewCondition to retrieve all control elements. AutomationElementCollection elementCollectionControl = elementMainWindow.FindAll( TreeScope.Subtree, Automation.ControlViewCondition); Console.WriteLine("\nAll control elements:"); foreach (AutomationElement autoElement in elementCollectionControl) { Console.WriteLine(autoElement.Current.Name); } } |
Visual Basic | ![]() |
---|---|
''' <summary> ''' Examples of using predefined conditions to find elements. ''' </summary> ''' <param name="elementMainWindow">The element for the target window.</param> Public Sub StaticConditionExamples(ByVal elementMainWindow As AutomationElement) If elementMainWindow Is Nothing Then Throw New ArgumentException() End If ' Use TrueCondition to retrieve all elements. Dim elementCollectionAll As AutomationElementCollection = elementMainWindow.FindAll(TreeScope.Subtree, Condition.TrueCondition) Console.WriteLine(vbLf + "All control types:") Dim autoElement As AutomationElement For Each autoElement In elementCollectionAll Console.WriteLine(autoElement.Current.Name) Next autoElement ' Use ContentViewCondition to retrieve all content elements. Dim elementCollectionContent As AutomationElementCollection = elementMainWindow.FindAll(TreeScope.Subtree, Automation.ContentViewCondition) Console.WriteLine(vbLf + "All content elements:") For Each autoElement In elementCollectionContent Console.WriteLine(autoElement.Current.Name) Next autoElement ' Use ControlViewCondition to retrieve all control elements. Dim elementCollectionControl As AutomationElementCollection = elementMainWindow.FindAll(TreeScope.Subtree, Automation.ControlViewCondition) Console.WriteLine(vbLf & "All control elements:") For Each autoElement In elementCollectionControl Console.WriteLine(autoElement.Current.Name) Next autoElement End Sub 'StaticConditionExamples |
Windows Vista
.NET Framework 和 .NET Compact Framework 並非對每一個平臺的全部版本都提供支持。有關支持的版本的列表,請參見.NET Framework 系統要求。