使用Windows.Automation,我可以通过regex定位AutomationElement吗

本文关键字:regex 定位 AutomationElement 可以通过 Windows Automation 使用 | 更新日期: 2023-09-27 17:59:30

我有一个对象树,它在表父级中有行对象。我正试图将所有这些行放入AutomationElementCollection

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

所有行的AutomationElement.NameProperty都包含字符串"row"。然而,它们是该字符串的变体,例如"Row1"、"Row2"、"TopRow"。。。

由于FindAll方法允许您定义TreeScope并查找任何与所提供的Condition参数匹配的AutomationElement,因此我似乎遗漏了一些内容。我只希望我的条件不受限制,因为我已经可以通过TreeScope控制查找范围。

使用Windows.Automation,我可以通过regex定位AutomationElement吗

//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;
}

正如文档所述,您可以要求进行不区分大小写的比较。没有"正则表达式"标志。您将不得不手动进行筛选。