在代码源中调用按钮事件 [英] Call a Button Event In Code Source

查看:72
本文介绍了在代码源中调用按钮事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好
我需要一些帮助

我创建了一个按钮事件,让我们这样说:

Hello
I need some Help with something

I created a button event let''s say this:

public void bt_piece_Click(object sender, RoutedEventArgs e)
        {

              }               




我需要在源代码中输入此事件!
但是我可以把什么作为参数!

当我叫它我会写




I need to enter this event in source code When I want to!
but what can I put as parameters !

When I call it I''ll Write

bt_piece_Click(sender, ?)  



我写什么而不是?"

如果有人有想法请帮助
感谢



What do I write instead of "?"

If anyone has an idea please help
Thanks

推荐答案

IMO,您不应以这种方式执行逻辑.在您按如下方式进行呼叫时,
IMO you shouldn''t execute the logic this way. While you make the call as follows
bt_piece_Click(sender, new RoutedEventArgs())

更好的方法是将逻辑和UI动作相互配合.

因此,例如,如果您有某种逻辑要从多个位置执行,请为此创建一个方法并从适当的位置调用它.像这样的东西:

a better way would be to separate the logic and the UI actions from each other.

So for example if you have some logic you want to execute from several places, create a method for that and call it from proper places. Something like:

void DoSomeStuff {
...
}
public void bt_piece_Click(object sender, RoutedEventArgs e)
{
  DoSomeStuff()
}
public void bt_OtherButton_Click(object sender, RoutedEventArgs e)
{
  DoSomeStuff()
}
...


如果为按钮后面的逻辑创建一个方法,然后从事件处理程序和您需要的任何其他方法中调用该方法,那就更好了.

It is much better if you create a method for the logic behind the button and call that method from the event handler and any other method you require.

private void button1(object sender, EventArg e)
{
   ButtonLogic();  // call from event handler
}

private void ButtonLogic()
{
  // code to do something
}

private void SomeOtherMethod()
{
   ButtonLogic(); // call from other method
}


您确实应该使用MVVM和ICommand模式绑定到按钮.这样,您就不必在视图中隐藏任何代码.您可以从视图模型处理此问题,然后随时在视图模型上调用命令:

参见此处的示例: http://tap-source.com/?p=160 [ ^ ]
You really should be using MVVM and the ICommand pattern to bind to the button. That way, you don''t have to have code behind in your view. You can handle this from a view model and then call the command on the view model any time you like:

See here for an example: http://tap-source.com/?p=160[^]


这篇关于在代码源中调用按钮事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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