微软的TAF技术,可以实现自动化测试,或是对其他进程进行UI操作。https://blog.csdn.net/jfyy/article/details/80700977
参考:https://www.codeproject.com/Articles/172391/UIAutomation-Coded-UI-Tests-AutomationPeer-and-WPF
https://docs.microsoft.com/en-us/dotnet/framework/wpf/controls/ui-automation-of-a-wpf-custom-control
为了在WPF里支持这套技术,微软使用了Peer对象,并为其标准控件(button,textblock)制作了对应的Peer类,例如ButtonAutomationPeer。并在控件的protected override AutomationPeer OnCreateAutomationPeer()方法里,实例化对应的Peer类。
但如果是我们制作的Customize控件,或使用第三方的控件,因为没有对应地制作Peer类,就不能支持TAF。下面的例子就是我在研究如何为第三方ActiPro的TreeListBox控件,制作Peer的过程。
1,先如下派生自己的TreeListBox,并实现OnCreateAutomationPeer()
public class AutomationTreeListBox : TreeListBox
{
protected override AutomationPeer OnCreateAutomationPeer()
{
return new TreeListAutomationPeer(this);
}
}
只要我们能够制作出TreeListAutomationPeer,就能完美支持TAF。
2, TreeListAutomationPeer基类的选择
没必要重复做轮子,选择恰当的AutomationPeer基类会减少很多工作量。
如果是制作的是一个简单Control, 可使用FrameworkElementAutomationPeer。
如果是制作一个TabControl,可使用TabControlAutomationPeer。
所以我第一时间想选择TreeViewAutomationPeer作为基类,可第三方ActiPro的TreeListBox控件是派生于ItemsControl