本文介绍了使用Windows.Automation,我可以找到由正则表达式的AutomationElement?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有了一个父表中的行对象的对象树。我试图把所有这些行到 AutomationElementCollection

  AutomationElementCollection航空自卫队= ParentTableObj.FindAll 

TreeScope.Children,
新PropertyCondition

AutomationElement.NameProperty,
:我想在这里使用正则表达式

);



所有行'的 AutomationElement.NameProperty 包含字符串行。然而,他们是该字符串的变化 - 例如, ROW1,的Row2,TopRow,...



好像我可能会丢失,因为的FindAll 方法允许你定义 TreeScope 并找到任何 AutomationElement ,它提供的<$ C $匹配C>条件参数。我只是想不受限制我的病情,因为我已经可以控制由 TreeScope 查找范围。


解决方案

  //例:
AutomationElement元= FindFirstDescendant(
AutomationElement.FromHandle(windows_hWnd),
(ELE)=> Regex.IsMatch(ele.Current.Name,图案)
);

//通用的方法找到一个后代元素:
公共静态AutomationElement FindFirstDescendant(AutomationElement元素,Func键< AutomationElement,布尔>条件){
VAR沃克= TreeWalker.ControlViewWalker ;
元素= walker.GetFirstChild(元);
,而(元素!= NULL){
如果(条件(元素))
返回元素;
无功子元素= FindFirstDescendant(元素,条件);
如果(子元素!= NULL)
返回子元素;
元素= walker.GetNextSibling(元);
}
返回NULL;
}


I have an object tree that has row objects within a table parent. I'm attempting to put all these rows into an AutomationElementCollection

AutomationElementCollection asdf = ParentTableObj.FindAll
     (
     TreeScope.Children,
     new PropertyCondition
          (
          AutomationElement.NameProperty,
          "I want to use regex here"
          )
     );

All of the rows' AutomationElement.NameProperty contains the string "row". However, they are variations of that string - e.g. "Row1", "Row2", "TopRow", ...

It seems like I may be missing something since the FindAll method allows you to define the TreeScope and find any AutomationElement, which matches the provided Condition parameter. I just want my condition to be unrestricted since I can already control the find scope by TreeScope.

解决方案
//Example :
AutomationElement element = FindFirstDescendant( 
    AutomationElement.FromHandle(windows_hWnd), 
    (ele)=>Regex.IsMatch( ele.Current.Name, pattern)
);

//The generic method to find a descendant element:
public static AutomationElement FindFirstDescendant(AutomationElement element, Func<AutomationElement, bool> condition) {
    var walker = TreeWalker.ControlViewWalker;
    element = walker.GetFirstChild(element);
    while (element != null) {
        if (condition(element))
            return element;
        var subElement = FindFirstDescendant(element, condition);
        if (subElement != null)
            return subElement;
        element = walker.GetNextSibling(element);
    }
    return null;
}

这篇关于使用Windows.Automation,我可以找到由正则表达式的AutomationElement?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 13:01