How to programmatically click a WPF button

178 篇文章 0 订阅

http://joshsmithonwpf.wordpress.com/2007/03/09/how-to-programmatically-click-a-button/


Back in WinForms it was really easy to simulate a userclicking a Button, you just call a Button’s PerformClick method.  For somereason the WPF Button does not have that method.  It turns out that youneed to enter into the automation world to accomplish this task.


WPF takes a slighly different approach than WinForms here.Instead of having the automation of an object built into the API, they have aseparate class for each object that is responsible for automating it. In thiscase you need the ButtonAutomationPeer to accomplish this task.


ButtonAutomationPeer peer = new ButtonAutomationPeer(someButton );

IInvokeProviderinvokeProv = peer.GetPatternPatternInterface.Invoke ) as IInvokeProvider;

invokeProv.Invoke();

 

The UIAutomation*.dll is located in this folder:

C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0

 

If you can't find in your Add Reference->.Net tab, then you have to use the Browse tabto go to the given path, and add the assembly (Right Click on the References, choose add reference, clickbrowse tab):


Other reference in Chinese:

http://blog.csdn.net/jhqin/article/details/6932753

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值