通过使用UIA验证 http://uiautomationverify.codeplex.com/,您可以看到 DoubleUpDown 控件被视为三个控件,没有层次结构信息和以下 AutomationId:
-
自动选择文本框
- Part_IncreaseButton
- Part_DecreaseButton
因此,您可以将它们作为普通控件进行自动化,但如果同一窗口中有多个 DoubleUpDown 控件,则会出现问题,因为所有控件都将具有相同的 AutomationId。
下面是一个示例应用程序,其中前两个文本框作为 DoubleUpDown 控件,第三个文本框作为专为自动化设计的自定义控件。
...
<Label Content="Label for DoubleUpDown1" Grid.Row="0" Grid.Column="0"
FontSize="15" Background="Aqua"/>
<xctk:DoubleUpDown Name="test1" Grid.Row="0" Grid.Column="1"
FormatString="F3" Value="1564.6749586" Increment=".001" Maximum="200000.599"
AutomationProperties.AutomationId="006" AutomationProperties.Name="NormalDoubleUpDown1" />
<Label Content="Label for DoubleUpDown2" Grid.Row="1" Grid.Column="0"
FontSize="15" Background="Aqua"/>
<xctk:DoubleUpDown Name="test2" Grid.Row="1" Grid.Column="1"
FormatString="F3" Value="1564.6749586" Increment=".001" Maximum="300000.751"
AutomationProperties.AutomationId="007" AutomationProperties.Name="NormalDoubleUpDown2" />
<Label Content="Label for MyDoubleUpDown" Grid.Row="2" Grid.Column="0" FontSize="15" Background="Aqua" />
<local:MyDoubleUpDown x:Name="test3" Grid.Row="2" Grid.Column="1"
FormatString="F3" Value="1564.7749586" Increment=".001" Maximum="200000.599"
AutomationProperties.AutomationId="008" AutomationProperties.Name="My Custom DoubleUpDown" />
...
在 UIA 验证中,正常的 DoubleUpDown 控件显示为具有相同的 AutomationId。
自定义的与真实的层次结构一起出现,并且可以使用在 XAML 中设置的 AutomationId(此处008).
自定义控件我的双上下,Xceed 的简单子类,但具有自动化对等体。
public class MyDoubleUpDown : Xceed.Wpf.Toolkit.DoubleUpDown
{
protected override AutomationPeer OnCreateAutomationPeer()
{
return new MyDoubleUpDownAutomationPeer(this);
}
}
public class MyDoubleUpDownAutomationPeer : FrameworkElementAutomationPeer
{
public MyDoubleUpDownAutomationPeer(MyDoubleUpDown owner)
: base(owner)
{
}
}
这是在窗口中自动化唯一的 DoubleUpDown 控件的默认方法。
// link to the application and retrieve the main window
Application application = Application.Attach("WpfTestApplication1");
var windows = application.GetWindows();
var window = windows.FirstOrDefault();
// get the child components
TextBox theEdit = window.Get<TextBox>("AutoSelectTextBox");
Button increaseButton = window.Get<Button>("PART_IncreaseButton");
Button decreaseButton = window.Get<Button>("PART_DecreaseButton");
// define the value
theEdit.SetValue("600");
// increase and decrease the value
increaseButton.Click();
increaseButton.Click();
increaseButton.Click();
decreaseButton.Click();
这是基于 Xceed 的自动化自定义控件的代码。
// retrieve the custom control
IUIItem theCustomControl = window.Get(SearchCriteria.ByAutomationId("008"));
// get the childs items
if(theCustomControl is CustomUIItem)
{
// retrieve the custom control container
IUIItemContainer foundCustomControl = (theCustomControl as CustomUIItem).AsContainer();
// get the child components
TextBox theEdit3 = foundCustomControl.Get<TextBox>("AutoSelectTextBox");
Button increaseButton3 = foundCustomControl.Get<Button>("PART_IncreaseButton");
Button decreaseButton3 = foundCustomControl.Get<Button>("PART_DecreaseButton");
// perform actions...
theEdit3.SetValue("800");
increaseButton3.Click();
increaseButton3.Click();
}