AutomationElement.FindFirst(TreeScope, Condition) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回与指定条件匹配的第一个子级或子代元素。
public:
System::Windows::Automation::AutomationElement ^ FindFirst(System::Windows::Automation::TreeScope scope, System::Windows::Automation::Condition ^ condition);
public System.Windows.Automation.AutomationElement FindFirst (System.Windows.Automation.TreeScope scope, System.Windows.Automation.Condition condition);
member this.FindFirst : System.Windows.Automation.TreeScope * System.Windows.Automation.Condition -> System.Windows.Automation.AutomationElement
Public Function FindFirst (scope As TreeScope, condition As Condition) As AutomationElement
参数
- scope
- TreeScope
一个按位值组合,用于指定搜索的范围。
- condition
- Condition
包含匹配条件的对象。
返回
满足条件的第一个元素,如果找不到匹配项,则为 null
。
示例
以下示例演示如何从子窗口的标识符中查找子窗口。
/// <summary>
/// Find a UI Automation child element by ID.
/// </summary>
/// <param name="controlName">Name of the control, such as "button1"</param>
/// <param name="parentElement">Parent element, such as an application window, or the
/// AutomationElement.RootElement when searching for the application window.</param>
/// <returns>The UI Automation element.</returns>
private AutomationElement FindChildElement(String controlName, AutomationElement rootElement)
{
if ((controlName == "") || (rootElement == null))
{
throw new ArgumentException("Argument cannot be null or empty.");
}
// Set a property condition that will be used to find the main form of the
// target application. In the case of a WinForms control, the name of the control
// is also the AutomationId of the element representing the control.
Condition propCondition = new PropertyCondition(
AutomationElement.AutomationIdProperty, controlName, PropertyConditionFlags.IgnoreCase);
// Find the element.
return rootElement.FindFirst(TreeScope.Element | TreeScope.Children, propCondition);
}
''' <summary>
''' Find a UI Automation child element by ID.
''' </summary>
''' <param name="controlName">Name of the control, such as "button1"</param>
''' <param name="rootElement">Parent element, such as an application window, or the
''' AutomationElement.RootElement when searching for the application window.</param>
''' <returns>The UI Automation element.</returns>
Private Function FindChildElement(ByVal controlName As String, ByVal rootElement As AutomationElement) _
As AutomationElement
If controlName = "" OrElse rootElement Is Nothing Then
Throw New ArgumentException("Argument cannot be null or empty.")
End If
' Set a property condition that will be used to find the main form of the
' target application. In the case of a WinForms control, the name of the control
' is also the AutomationId of the element representing the control.
Dim propCondition As New PropertyCondition(AutomationElement.AutomationIdProperty, _
controlName, PropertyConditionFlags.IgnoreCase)
' Find the element.
Return rootElement.FindFirst(TreeScope.Element Or TreeScope.Children, propCondition)
End Function 'FindChildElement
注解
搜索范围相对于调用方法的元素。
在桌面上搜索顶级窗口时,请务必在 中scope
指定 Children ,而不是 Descendants。 在桌面的整个子树中搜索可能会循环访问数千个项,并导致堆栈溢出。
如果客户端应用程序可能尝试在其自己的用户界面中查找元素,则必须在单独的线程上进行所有UI 自动化调用。