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

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

问题描述

我想以编程方式在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.

Raiseevent

Raiseevent

推荐答案

这是操作方法。 ..

Here's how to do it...

假设您在 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"/>

然后在您想要点击的位置后面的代码中完成事件处理程序代码将通过编程方式触发:

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. :)

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

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

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