3

我需要使用 UIAutomation 从外部应用程序检索 Datagrid 中的所有项目。目前,我只能检索(并在 UISpy 中查看)可见项目。有没有办法缓存 Datagrid 中的所有项目然后拉取它们?这是代码:

static public ObservableCollection<Login> GetLogins()
    {

        ObservableCollection<Login> returnLogins = new ObservableCollection<Login>();

        var id = System.Diagnostics.Process.GetProcessesByName("<Name here>")[0].Id;
        var desktop = AutomationElement.RootElement;

        var bw = AutomationElement.RootElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ProcessIdProperty, id));

        var datagrid = bw.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "lv"));

        var loginLines = datagrid.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.DataItem));

        foreach (AutomationElement loginLine in loginLines)
        {
            var loginInstance = new Login { IP = new IP() };

            var loginLinesDetails = loginLine.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Custom));

            for (var i = 0; i < loginLinesDetails.Count; i++)
            {
                var cacheRequest = new CacheRequest 
                { 
                    AutomationElementMode = AutomationElementMode.None,
                    TreeFilter = Automation.RawViewCondition
                };

                cacheRequest.Add(AutomationElement.NameProperty);
                cacheRequest.Add(AutomationElement.AutomationIdProperty);

                cacheRequest.Push();

                var targetText = loginLinesDetails[i].FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "TextBlock"));

                cacheRequest.Pop();

                var myString = targetText.Cached.Name;

                #region Determine data and write to return object
                //Removed private information
                #endregion
                }

            }

            returnLogins.Add(loginInstance);
        }

        return returnLogins;
    }
4

2 回答 2

2

您只能检索可见单元格,因为您启用了表虚拟化。

尝试禁用虚拟化(并非总是在所有应用程序中都可以,但也许您想将其移入配置并在测试之前对其进行更改)

于 2012-08-27T23:58:04.643 回答
1

我 99% 确定这是不可能的。UI 自动化不知道由网格的当前可见部分表示的数据结构。它只看到可见的东西。我认为您将不得不翻阅网格以获取所有数据(这就是我所做的)。

于 2012-08-29T15:40:08.047 回答