【问题标题】:Getting IUIItem[] for children of CustomUIItem in TestStack.White在 TestStack.White 中为 CustomUIItem 的子项获取 IUIItem[]
【发布时间】:2017-01-26 19:08:46
【问题描述】:

一个 WPF 应用程序正在使用一个应用程序框架,我无法编辑它们。

我可以通过以下方式访问 GUI 中的每个元素:

IUIItem[] items = window.GetMultiple(SearchCriteria.All);
foreach (var item in items)
{
    visit((dynamic)item);
}

我对正常控制没有任何问题,但我用CustomUIItem 碰壁了。

我想访问它的所有孩子,但我无法从他们那里创建一个新数组IUIItem[]。

这是我现在拥有的:

    void visit(CustomUIItem item)
    {
        AutomationElementCollection children =
            item
            .AutomationElement
            .FindAll(TreeScope.Children, Condition.TrueCondition);
        UIItemCollection temp = new UIItemCollection(children.Cast<AutomationElement>());
        foreach(var t in temp)
        {
            visit((dynamic)t);
        }
    }

这有时会抛出,并且大部分时间集合仍然是空的。

CusomControl 在其子项中具有“正常”控件。 我想要那些普通的IUIItems。

我在哪里可以找到这方面的文档。 我唯一找到的是this,我不能这样做,因为我只是从外面访问,我不知道控件的内容。

【问题讨论】:

    标签: c# wpf white-framework


    【解决方案1】:

    如果我真的理解你的问题。

        IUIItem[] items = window.GetMultiple(SearchCriteria.All);
        foreach (var item in items)
        {
            visit(item);
        }
    

    我已经更新了你的 visit() 方法,它现在需要一个 IUItem 作为参数来允许访问普通和自定义控件。

        public void visit(IUIItem item)
        {
            if (item is CustomUIItem)
            {
                // Process custom controls
                CustomUIItem customControl = item as CustomUIItem;
    
                // Retrieve all the child controls
                IUIItem[] items = customControl.AsContainer().GetMultiple(SearchCriteria.All);
    
                // visit all the children
                foreach (var t in items)
                {
                    visit(t);
                }
                ...
            }
            else
            {
                // Process normal controls
                ...
            }
        }
    

    【讨论】:

    • 您没有使用(动态)关键字。但我认为使用 AsContainer 是我一直在寻找的。​​span>
    • 好的,我刚刚假设 visit() 方法的目标只是以递归方式浏览 UIItems 树。现在我看到你真正的 visit() 方法可能比给定的代码更复杂;-)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-17
    • 1970-01-01
    • 2019-07-22
    • 2016-04-20
    • 2016-07-25
    • 2019-09-26
    • 1970-01-01
    相关资源
    最近更新 更多