在 C# 和 Xaml 中以编程方式单击 Windows 8 按钮 [英] Windows 8 Button click programmatically in C# and Xaml

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

问题描述

我想在 C# xaml 中以编程方式单击按钮.我发现没有像 PerformClick 这样的方法.还有什么可以替代它.

I want to click button programatically in C# xaml. As I found that there is no method like PerformClick. What else is the alternative for it.

实际上我有一组 10 个带有 x:Name 的按钮,例如 btn1、btn2、btn3 等...根据我需要单击这些按钮的数字,我有一个从 1 到 10 的数字.我打算通过使用

Actually I have set of 10 buttons with x:Name like btn1, btn2, btn3 etc... I have a number from 1 to 10 based on that number I need to click these buttons. I was planning to get the control by using

ParentControl.FindName('btn' + number)

然后从中触发事件.但是由于 Windows 8 C# 中没有 PerformClick 方法,因此我需要一个替代方法.

and then fire the event from that. But since there is no PerformClick method in Windows 8 C# hence i need an alternative for this.

引发事件

推荐答案

这里是如何做...

假设您在 XAML 文件中定义了此按钮:

Let's say you have this button defined in your XAML file:

<Button Grid.Column="0" Grid.Row="0" x:Name="DefaultSnooze" Content="Default Snooze" Click="SendToast_Click" Height="34" Width="134"/>

然后在您希望以编程方式触发 Click 事件处理程序代码的后面的代码中执行此操作:

Then do this in your code behind where you want the Click event handler code to be fired programmatically:

ButtonAutomationPeer peer = new ButtonAutomationPeer(DefaultSnooze);

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

我现在对其进行了测试,效果很好.:)

I tested it right now and it works beautifully. :)

来源:如何以编程方式单击按钮

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

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