[英]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.