TreeWalker and FindAll function[转贴]

http://www.automationspy.com/post-25_12_2014.html

Get all control children of an element using AutomationElement.FindAll function:

 1 // using FindAll function
 2 private List<AutomationElement> GetChildren(AutomationElement parent)
 3 {
 4     if (parent == null)
 5     {
 6         // null parameter
 7         throw new ArgumentException();
 8     }
 9     AutomationElementCollection collection = parent.FindAll(TreeScope.Children, Condition.TrueCondition);
10     if (collection != null)
11     {
12         List<AutomationElement> result = new List<AutomationElement>(collection.Cast<AutomationElement>());
13         return result;
14     }
15     else
16     {
17         // some error occured
18         return null;
19     }
20 }

Get control children of an element using the predefined TreeWalker.ControlViewWalker:

 

// using predefined TreeWalker.ControlViewWalker
private List<AutomationElement> GetChildren(AutomationElement parent)
{
    if (parent == null)
    {
        // null parameter
        throw new ArgumentException();
    }
    List<AutomationElement> result = new List<AutomationElement>();
    // the predefined tree walker wich iterates through controls
    TreeWalker tw = TreeWalker.ControlViewWalker;
    AutomationElement child = tw.GetFirstChild(parent);
    while (child != null)
    {
        result.Add(child);
        child = tw.GetNextSibling(child);
    }
    return result;
}

Get the same result using a custom TreeWalker:

 

 1 // using customized TreeWalker
 2 private List<AutomationElement> GetChildren(AutomationElement parent)
 3 {
 4     if (parent == null)
 5     {
 6         throw new ArgumentException();
 7     }
 8     List<AutomationElement> result = new List<AutomationElement>();
 9     PropertyCondition condition = new PropertyCondition(AutomationElement.IsControlElementProperty, true);
10     TreeWalker tw = new TreeWalker(condition);
11     AutomationElement child = tw.GetFirstChild(parent);
12     while (child != null)
13     {
14         result.Add(child);
15         child = tw.GetNextSibling(child);
16     }
17     return result;
18 }

 

MS UI Automation has two ways of navigating through collections of elements and hierarchies. One is by using FindAll function and can be used to obtain the entire collection or a subset of children/descendants of an Automation Element and the second way is by using TreeWalker. Both ways support search filtering by element properties. TW offers a lot more search features and navigation features like moving to first child, moving to last child, moving to next sibling of a specified element and moving to Parent.

    What is the connection between TreeWalker and FindAll function? FindAll is the same with using the predefined tree walker TW.ControlViewWalker and moving from the first child to the last child. So, FindAll function returns only control elements.

    There are 2 other predefined tree walkers: RawViewWalker and ContentViewWalker. RawViewWalker gets all the elements and ContentViewWalker gets elements marked as content elements.

    If you are using a similarity between TW.RawViewWalker or TW.ContentViewWalker and FindAll function you may find some incompatibilities meaning that you may find some Automation elements in one collection that don't have correspondents in the other collection. I think FindAll function can be used safely with TW.ControlViewWalker as 1-1.

    Why to use FindAll and why to use TreeWalker or why not always to use TreeWalker? Microsoft says FindAll function works faster than TreeWalker for some kinds of applications, like WPF applications, and TreeWalker works faster for Win32 controls. Could be, but the difference of speed may not be so big as you expect. FindAll function has also the advantage of making things easy when you want to search through the entire collection of descendants. TreeWalker can also be used to search through the collection of descendants (recursively), but with more code and less efficiency.

    Working with UI Automation may be challenging when using large collections of children or descendants and especially when using filtering criteria because these kinds of actions can be very time-consuming. So, understanding how to work with collections and navigating through hierarchies of elements is a big step in starting development with Microsoft UI Automation.

转载于:https://www.cnblogs.com/Luffyhuang/p/4566576.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值