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