TreeWalker.GetFirstChild 方法

定义

检索指定 AutomationElement 的第一个子元素。

重载

GetFirstChild(AutomationElement)

检索指定 AutomationElement 的第一个子元素。

GetFirstChild(AutomationElement, CacheRequest)

检索指定的 AutomationElement 的第一个子元素并缓存属性和模式。

注解

可以 AutomationElement 具有与当前视图条件不匹配的其他子元素,因此在导航元素树时不会返回。

树的结构 AutomationElement 随着可见用户界面 (UI) 桌面更改中的元素而更改。 不保证作为第一个子元素返回的元素将作为后续传递的第一个子元素返回。

GetFirstChild(AutomationElement)

检索指定 AutomationElement 的第一个子元素。

public:
 System::Windows::Automation::AutomationElement ^ GetFirstChild(System::Windows::Automation::AutomationElement ^ element);
public System.Windows.Automation.AutomationElement GetFirstChild (System.Windows.Automation.AutomationElement element);
member this.GetFirstChild : System.Windows.Automation.AutomationElement -> System.Windows.Automation.AutomationElement
Public Function GetFirstChild (element As AutomationElement) As AutomationElement

参数

element
AutomationElement

从中检索第一个子级的元素。

返回

AutomationElement

第一个子元素;如果不存在此类元素,则为空引用(在 Visual Basic 中为 Nothing)。

示例

以下示例演示如何 GetFirstChild 在子树中构造元素的树视图。

/// <summary>
/// Walks the UI Automation tree and adds the control type of each element it finds 
/// in the control view to a TreeView.
/// </summary>
/// <param name="rootElement">The root of the search on this iteration.</param>
/// <param name="treeNode">The node in the TreeView for this iteration.</param>
/// <remarks>
/// This is a recursive function that maps out the structure of the subtree beginning at the
/// UI Automation element passed in as rootElement on the first call. This could be, for example,
/// an application window.
/// CAUTION: Do not pass in AutomationElement.RootElement. Attempting to map out the entire subtree of
/// the desktop could take a very long time and even lead to a stack overflow.
/// </remarks>
private void WalkControlElements(AutomationElement rootElement, TreeNode treeNode)
{
    // Conditions for the basic views of the subtree (content, control, and raw) 
    // are available as fields of TreeWalker, and one of these is used in the 
    // following code.
    AutomationElement elementNode = TreeWalker.ControlViewWalker.GetFirstChild(rootElement);

    while (elementNode != null)
    {
        TreeNode childTreeNode = treeNode.Nodes.Add(elementNode.Current.ControlType.LocalizedControlType);
        WalkControlElements(elementNode, childTreeNode);
        elementNode = TreeWalker.ControlViewWalker.GetNextSibling(elementNode);
    }
}
''' <summary>
''' Walks the UI Automation tree and adds the control type of each element it finds 
''' in the control view to a TreeView.
''' </summary>
''' <param name="rootElement">The root of the search on this iteration.</param>
''' <param name="treeNode">The node in the TreeView for this iteration.</param>
''' <remarks>
''' This is a recursive function that maps out the structure of the subtree beginning at the
''' UI Automation element passed in as rootElement on the first call. This could be, for example,
''' an application window.
''' CAUTION: Do not pass in AutomationElement.RootElement. Attempting to map out the entire subtree of
''' the desktop could take a very long time and even lead to a stack overflow.
''' </remarks>
Private Sub WalkControlElements(ByVal rootElement As AutomationElement, ByVal treeNode As TreeNode)
    ' Conditions for the basic views of the subtree (content, control, and raw) 
    ' are available as fields of TreeWalker, and one of these is used in the 
    ' following code.
    Dim elementNode As AutomationElement = TreeWalker.ControlViewWalker.GetFirstChild(rootElement)

    While (elementNode IsNot Nothing)
        Dim childTreeNode As TreeNode = treeNode.Nodes.Add(elementNode.Current.ControlType.LocalizedControlType)
        WalkControlElements(elementNode, childTreeNode)
        elementNode = TreeWalker.ControlViewWalker.GetNextSibling(elementNode)
    End While

End Sub

注解

可以 AutomationElement 具有与当前视图条件不匹配的其他子元素,因此在导航元素树时不会返回。

树的结构 AutomationElement 随着可见用户界面 (UI) 桌面更改中的元素而更改。 不保证作为第一个子元素返回的元素将作为后续传递的第一个子元素返回。

另请参阅

适用于

GetFirstChild(AutomationElement, CacheRequest)

检索指定的 AutomationElement 的第一个子元素并缓存属性和模式。

public:
 System::Windows::Automation::AutomationElement ^ GetFirstChild(System::Windows::Automation::AutomationElement ^ element, System::Windows::Automation::CacheRequest ^ request);
public System.Windows.Automation.AutomationElement GetFirstChild (System.Windows.Automation.AutomationElement element, System.Windows.Automation.CacheRequest request);
member this.GetFirstChild : System.Windows.Automation.AutomationElement * System.Windows.Automation.CacheRequest -> System.Windows.Automation.AutomationElement
Public Function GetFirstChild (element As AutomationElement, request As CacheRequest) As AutomationElement

参数

element
AutomationElement

从中检索第一个子级的元素。

request
CacheRequest

一个缓存请求对象,它指定返回的 AutomationElement 上要进行缓存的属性和模式。

返回

AutomationElement

第一个子元素;如果不存在此类元素,则为空引用(在 Visual Basic 中为 Nothing)。

注解

可以 AutomationElement 具有与当前视图条件不匹配的其他子元素,因此在导航元素树时不会返回。

树的结构 AutomationElement 随着可见用户界面 (UI) 桌面更改中的元素而更改。 不保证作为第一个子元素返回的元素将作为后续传递的第一个子元素返回。

另请参阅

适用于