[英]Silverlight: Simulate RadioButton Click event using UI Automation?
在Silverlight中,由于Button
和RadioButton
控件都继承自System.Windows.Controls.Primitives.ButtonBase
,因此它们都具有Click
事件。
如果要模拟Button
Click
事件,则可以使用ButtonAutomationPeer
类,就像这样(给定名为myButton
的按钮):
ButtonAutomationPeer peer = new ButtonAutomationPeer(myButton);
IInvokeProvider ip = (IInvokeProvider)peer;
ip.Invoke();
但是,当我们尝试对RadioButton
做同样的事情时,我们发现RadioButtonAutomationPeer
类未实现IInvokeProvider
(因此我们无法调用Invoke()
)。 还有其他方法可以导致触发RadioButton
的Click
事件吗?
尝试这个:
RadioButtonAutomationPeer peer = new RadioButtonAutomationPeer(myRadioButton);
IToggleProvider tp = (IToggleProvider) peer;
while (tp.ToggleState != targetToggleState)
{
tp.Toggle();
}
您需要知道所需的切换状态(打开,关闭或不确定)。 或者,如果您只想切换到其他状态,只需调用Toggle即可触发while循环。
显然,这与单击并不完全相同。 如果将单选按钮绑定到命令,则可能会被迫更加明确,并执行以下操作以使自动化工作:
<RadioButton ... (don't set the Command property)>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<i:InvokeCommandAction Command="{Binding MyCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</RadioButton>
使用Blend SDK(即“ i:” xmlns),您在选中RadioButton时正在调用命令。 就我个人而言,即使没有自动将命令绑定到RadioButton的方式,我也会采用这种方法:尚不清楚何时使用绑定执行命令,但是触发器增加了一些清晰度,并消除了我下次查看时的思考编码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.