查找列表项的 UI 自动化元素

注意注意

本文档的目标读者是欲使用 System.Windows.Automation 命名空间中定义的托管 UI Automation类的 .NET Framework 开发人员。有关 UI Automation的最新信息,请参见 Windows Automation API: UI Automation(Windows 自动化 API:UI 自动化)。

本主题演示在已知某个列表项的索引的情况下如何在列表中检索该项的 AutomationElement

示例

下面的示例演示从列表中检索指定项的两种方法:一种方法是使用 TreeWalker,另一种方法是使用 FindAll

对于 Win32 控件,第一种方法往往更快,但对于 Windows Presentation Foundation (WPF) 控件,第二种方法更快。

''' <summary>
''' Retrieves an element in a list, using TreeWalker.
''' </summary>
''' <param name="parent">The list element.</param>
''' <param name="index">The index of the element to find.</param>
''' <returns>The list item.</returns>
Function FindChildAt(ByVal parent As AutomationElement, ByVal index As Integer) As AutomationElement

    If (index < 0) Then
        Throw New ArgumentOutOfRangeException()
    End If
    Dim walker As TreeWalker = TreeWalker.ControlViewWalker
    Dim child As AutomationElement = walker.GetFirstChild(parent)
    For x As Integer = 1 To (index - 1)
        child = walker.GetNextSibling(child)
        If child = Nothing Then

            Throw New ArgumentOutOfRangeException()
        End If
    Next x
    Return child
End Function

''' <summary>
''' Retrieves an element in a list, using FindAll.
''' </summary>
''' <param name="parent">The list element.</param>
''' <param name="index">The index of the element to find.</param>
''' <returns>The list item.</returns>
Function FindChildAtB(ByVal parent As AutomationElement, ByVal index As Integer) As AutomationElement
    Dim findCondition As Condition = _
        New PropertyCondition(AutomationElement.IsControlElementProperty, True)
    Dim found As AutomationElementCollection = parent.FindAll(TreeScope.Children, findCondition)
    If (index < 0) Or (index >= found.Count) Then
        Throw New ArgumentOutOfRangeException()
    End If
    Return found(index)
End Function
/// <summary>
/// Retrieves an element in a list, using TreeWalker.
/// </summary>
/// <param name="parent">The list element.</param>
/// <param name="index">The index of the element to find.</param>
/// <returns>The list item.</returns>
AutomationElement FindChildAt(AutomationElement parent, int index)
{
    if (index < 0)
    {
        throw new ArgumentOutOfRangeException();
    }
    TreeWalker walker = TreeWalker.ControlViewWalker;
    AutomationElement child = walker.GetFirstChild(parent);
    for (int x = 1; x <= index; x++)
    {
        child = walker.GetNextSibling(child);
        if (child == null)
        {
            throw new ArgumentOutOfRangeException();
        }
    }
    return child;
}

/// <summary>
/// Retrieves an element in a list, using FindAll.
/// </summary>
/// <param name="parent">The list element.</param>
/// <param name="index">The index of the element to find.</param>
/// <returns>The list item.</returns>
AutomationElement FindChildAtB(AutomationElement parent, int index)
{
    Condition findCondition = new PropertyCondition(AutomationElement.IsControlElementProperty, true);
    AutomationElementCollection found = parent.FindAll(TreeScope.Children, findCondition);
    if ((index < 0) || (index >= found.Count))
    {
        throw new ArgumentOutOfRangeException();
    }
    return found[index];
}

请参见

概念

获取 UI 自动化元素