获取一个DataGrid使用UIAutomation的全部内容 [英] Getting full contents of a Datagrid using UIAutomation

查看:467
本文介绍了获取一个DataGrid使用UIAutomation的全部内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用UIAutomation外部应用程序检索所有在DataGrid中的项目。目前,我只能检索(并查看UISpy)的可见的项目。有没有一种方法来缓存所有的DataGrid中的项目,然后将它们拉?这里的code:

 静态公网的ObservableCollection<登录> GetLogins()
    {

        的ObservableCollection<登录> returnLogins =新的ObservableCollection<登录>();

        VAR ID = System.Diagnostics.Process.GetProcessesByName(<名称在这里与GT;)[0] .ID;
        VAR桌面= AutomationElement.RootElement;

        VAR体重= AutomationElement.RootElement.FindFirst(TreeScope.Children,新PropertyCondition(AutomationElement.ProcessIdProperty,ID));

        VAR数据网格= bw.FindFirst(TreeScope.Children,新PropertyCondition(AutomationElement.AutomationIdProperty,LV));

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

        的foreach(的AutomationElement loginLine在loginLines)
        {
            VAR loginInstance =新的登录{IP =新的IP()};

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

            对于(VAR I = 0; I< loginLinesDetails.Count;我++)
            {
                VAR cacheRequest =新CacheRequest
                {
                    AutomationElementMode = AutomationElementMode.None,
                    TreeFilter = Automation.RawViewCondition
                };

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

                cacheRequest.Push();

                VAR targetText = loginLinesDetails [I] .FindFirst(TreeScope.Children,新PropertyCondition(AutomationElement.ClassNameProperty的TextBlock));

                cacheRequest.Pop();

                VAR MyString的= targetText.Cached.Name;

                #REGION确定的数据并写入返回对象
                //删除私人信息
                #endregion
                }

            }

            returnLogins.Add(loginInstance);
        }

        返回returnLogins;
    }
 

解决方案

您可以只是因为你对表的虚拟化检索可见单元格。

尝试禁用虚拟化(并不总是能够在所有的应用程序,但也许你想要将其移动到配置和测试之前改变它)

I have need to retrieve all of the items in a Datagrid from an external application using UIAutomation. Currently, I can only retrieve (and view in UISpy) the visible items. Is there a way to cache all of the items in the Datagrid and then pull them? Here's the code:

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

解决方案

You can only retrieve the visible cells because you have table virtualization on.

Try disabling the virtualization (not always possible in all application but perhaps you want to move it into configuration and change it before testing)

这篇关于获取一个DataGrid使用UIAutomation的全部内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆