Server-Side UI Automation Provider - WPF Sample

Server-Side UI Automation Provider - WPF Sample

2014-09-14

引用程序集

自动化对等类

WPF Sample

参考

 

引用程序集


 返回 

  • UIAutomationProviders.dll
  • UIAutomationTypes.dll 
  • WindowsBase.dll

自动化对等类[1]


 返回

WPF 控件通过派生自 AutomationPeer 的对等类的树来支持 UI 自动化。  按照约定,对等类的名称须以控件类的名称开头,并以“AutomationPeer”结尾。 例如,ButtonAutomationPeer 是 Button 控件类的对等类。 这些对等类基本等效于 UI 自动化控件类型,但专用于 WPF 元素。 通过 UI 自动化接口访问 WPF 应用程序的自动化代码不直接使用自动化对等类,但同一进程空间中的自动化代码可以直接使用自动化对等类。

Server-Side UI Automation Provider - WPF Sample_第1张图片

图1 ButtonAutomationPeer metadata

WPF Sample[2]


 返回

示例代码如下:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Windows;
 6 using System.Windows.Controls;
 7 using System.Windows.Data;
 8 using System.Windows.Documents;
 9 using System.Windows.Input;
10 using System.Windows.Media;
11 using System.Windows.Media.Imaging;
12 using System.Windows.Navigation;
13 using System.Windows.Shapes;
14 using System.Windows.Automation.Peers;
15 using System.Windows.Automation.Provider;
16 namespace WpfServerProvider
17 {
18     /// <summary>
19     /// Interaction logic for MainWindow.xaml
20     /// </summary>
21     public partial class MainWindow : Window
22     {
23         public MainWindow()
24         {
25             InitializeComponent();
26             UIAButton btn = new UIAButton();
27             btn.Content = "TestButton";
28             this.Content = btn;
29         }
30     }
31     //My WPF button class, derive from WPF Button class
32     public class UIAButton : Button
33     {
34         //Override this function to return my derived AutomationPeer class
35         protected override AutomationPeer OnCreateAutomationPeer()
36         {
37             return new UIAButtonAutomationPeer(this);
38         }
39     }
40     //My AutomationPeer class
41     //Add implementation of UI ValuePattern comparing with base implementation    
42     public class UIAButtonAutomationPeer : ButtonAutomationPeer, IValueProvider
43     {
44         //owner parameter is the WPF Button instance
45         public UIAButtonAutomationPeer(Button owner)
46             : base(owner)
47         {
48         }
49         //Return UIA Name property
50         protected override string GetNameCore()
51         {
52             string originalName = base.GetNameCore();
53             return string.Format("{0} {1}", originalName, DateTime.Now.ToLongTimeString());
54         }
55         //Return ValuePattern interface        
56         public override object GetPattern(PatternInterface patternInterface)
57         {
58             if (patternInterface == PatternInterface.Value)
59             {
60                 return this;
61             }
62             return null;
63         }
64         public bool IsReadOnly { get { return true; } }
65         //ValuePattern's implementation
66         public string Value
67         {
68             get
69             {
70                 return string.Format("Height={0},Wideth={1}",
71                     Owner.RenderSize.Height, Owner.RenderSize.Width);
72             }
73         }
74         public void SetValue(string value) { }
75     }
76 }
View Code

Server-Side UI Automation Provider - WPF Sample_第2张图片

图2 UISpy to WPF provider sample

参考

[1] WPF 自定义控件的 UI 自动化
[2] UI Automation -- Under the Hood

 

你可能感兴趣的:(automation)