简体   繁体   English

WP8 C#按钮更改单击

[英]WP8 C# Button Change Click

I am programming in C# for Windows Phone 8 and have a Windows.System.Controls.Button object which does not have a "Click" event defined in the XML upon page load. 我正在使用C#进行Windows Phone 8编程,并且有一个Windows.System.Controls.Button对象,它在页面加载时没有在XML中定义的“Click”事件。 However, after certain pieces of data are obtained, I want to set the button's click event to a specific function I have written. 但是,在获得某些数据后,我想将按钮的click事件设置为我编写的特定函数。

How can this be done? 如何才能做到这一点? The "OnClick" event is protected, and I can't find which setting to change in the MSDN site. “OnClick”事件受到保护,我无法在MSDN站点中找到要更改的设置。

从代码中你应该可以做一些像MyButton.Click += clickhandler ,从XAML你也可以做到这一点。

You can potentially change the click event of the button by doing the following: 您可以通过执行以下操作来更改按钮的单击事件:

btButton.Click += NewHandler()

Another way, which isn't as clean would be to have a flag that checks if you have reached a specific stage, declared globally. 另一种不那么干净的方法是使用一个标志来检查你是否已达到全局声明的特定阶段。

bool myBool = false;

Then you can change the bool flag to true and check whether it's true in the click event: 然后你可以将bool标志更改为true并检查click事件中是否为true:

if(myBool)
{
   //toDo Logic
}

This way you won't need to create another event handler. 这样您就不需要创建另一个事件处理程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM