其他分享
首页 > 其他分享> > CodeGo.net>使用UIAutomation获取Datagrid的全部内容

CodeGo.net>使用UIAutomation获取Datagrid的全部内容

作者:互联网

我需要使用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;
    }

解决方法:

由于已启用表虚拟化,因此只能检索可见的单元格.

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

标签:ui-automation,c,net
来源: https://codeday.me/bug/20191127/2076486.html