如何以编程方式单击 WPF 中的按钮? [英] How to programmatically click a button in WPF?

查看:30
本文介绍了如何以编程方式单击 WPF 中的按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

既然 WPF 中没有 button.PerformClick() 方法,那么有没有办法以编程方式单击 WPF 按钮?

Since there's no button.PerformClick() method in WPF, is there a way to click a WPF button programmatically?

推荐答案

WPF 在此采用与 WinForms 略有不同的方法.他们没有将对象的自动化内置到 API 中,而是为每个负责自动化的对象都有一个单独的类.在这种情况下,您需要 ButtonAutomationPeer 来完成此任务.

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

ButtonAutomationPeer peer = new ButtonAutomationPeer(someButton);
IInvokeProvider invokeProv = peer.GetPattern(PatternInterface.Invoke) as IInvokeProvider;
invokeProv.Invoke();

这里是关于这个主题的博文.

Here is a blog post on the subject.

注意:IInvokeProvider 接口在 UIAutomationProvider 程序集中定义.

Note: IInvokeProvider interface is defined in the UIAutomationProvider assembly.

这篇关于如何以编程方式单击 WPF 中的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆