在发送事件Dispatchertimer.Tick一个额外的参数 [英] send a extra argument in Dispatchertimer.Tick event

查看:1153
本文介绍了在发送事件Dispatchertimer.Tick一个额外的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是如何在Dispatchertimer.Tick事件
这里给一些参数是代码:我想是在接受dispatcheTimer_Tick



一个整数值

  dispatcherTimer.Tick + =新的EventHandler(dispatcherTimer_Tick); 

私人无效dispatcherTimer_Tick(对象发件人,EventArgs五)
{
//.Text = DateTime.Now.Second.ToString();

}



我想要做的就是这样的事情。



 私人无效dispatcherTimer_Tick(对象发件人,EventArgs的发送,int类型的)
{
//.Text =日期时间。 Now.Second.ToString();

}



如何从调用点?? <发送的值/ p>

解决方案

虽然有许多方法,我觉得最方便使用匿名方法来关闭在执行此操作时变量:

  dispatcherTimer.Tick + =(S,参数)=> myControl.Text = DateTime.Now.Second.ToString(); 

如果你有行了一把,你还可以做更多的东西,如:

  INT数= 5; 
dispatcherTimer.Tick + =(S,参数)=>
{
字符串值=的someMethod();
串otherValue = DateTime.Now.Second.ToString()
myControl.Text =值+ otherValue +号;
}

如果您已经不仅仅是一个行屈指可数更多,那么你可能还想要另一个方法,但你可以使用lambda调用该方法:

  int值= 5; 
dispatcherTimer.Tick + =(S,参数)=> myRealMethod(someControl,值);

公共无效myRealMethod(控制someControl,int值)
{
someControl.Text =价值;
}

这时候你不便于事件处理程序的委托都忽略参数吨需要他们(我几乎从来不使用发件人,我通过实际的对象自己,如果我需要它,这样它不会转换为对象第一),以及额外的局部变量添加。


my question is how can i send some arguments in Dispatchertimer.Tick event here is the code: what i wanted to is receive a integer value at dispatcheTimer_Tick

    dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);

    private void dispatcherTimer_Tick(object sender, EventArgs e)
    {
        //.Text = DateTime.Now.Second.ToString();

    }

what i wanted to do is something like this

    private void dispatcherTimer_Tick(object sender, EventArgs e,int a)
    {
        //.Text = DateTime.Now.Second.ToString();

    }

how to send a value from a calling point??

解决方案

While there are a number of ways, I find it most convenient to use anonymous methods to close over variables when doing this:

dispatcherTimer.Tick += (s, args) => myControl.Text = DateTime.Now.Second.ToString();

If you have a handful of lines you could also do something more like:

int number = 5;
dispatcherTimer.Tick += (s, args) => 
{
    string value = someMethod();
    string otherValue = DateTime.Now.Second.ToString()
    myControl.Text = value + otherValue + number;
}

If you have more than just a handful of lines then you probably still want another method, but you can use a lambda to call that method:

int value = 5;
dispatcherTimer.Tick += (s, args) => myRealMethod(someControl, value);

public void myRealMethod(Control someControl, int value)
{
    someControl.Text = value;
}

This is convenient for both ignoring parameters of the event handler's delegate when you don't need them (I almost never use sender, I pass the actual object myself if I need it so that it's not cast to object first.) as well as adding in additional local variables.

这篇关于在发送事件Dispatchertimer.Tick一个额外的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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