传递参数事件处理程序

传递参数事件处理程序

本文介绍了传递参数事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下的事件处理程序来,我增加了一个参数 MusicNote 音乐:

I have the following EventHandler to which I added a parameter MusicNote music:

public void PlayMusicEvent(object sender, EventArgs e,MusicNote music)
{
    music.player.Stop();
    System.Timers.Timer myTimer = (System.Timers.Timer)sender;
    myTimer.Stop();
}

我需要处理程序添加到定时像这样:

myTimer.Elapsed += new ElapsedEventHandler(PlayMusicEvent(this, e, musicNote));

但得到的错误:

方法名称预计

编辑:?在这种情况下,我刚刚从包含该code片段的方法,我将如何传递通过E中的计时器自己的 EventArgs的

In this case I just pass e from the method which contains this code snippet, how would I pass the timer's own EventArgs?

推荐答案

Timer.Elapsed 预计特定签名的方法(带参数对象 EventArgs的)。如果你想用你的 PlayMusicEvent 方法,额外参数事件注册,你可以使用拉姆达前pression作为适配器时评估

Timer.Elapsed expects method of specific signature (with arguments object and EventArgs). If you want to use your PlayMusicEvent method with additional argument evaluated during event registration, you can use lambda expression as an adapter:

myTimer.Elapsed += new ElapsedEventHandler((sender, e) => PlayMusicEvent(sender, e, musicNote));

编辑::您还可以用较短的版本:

you can also use shorter version:

myTimer.Elapsed += (sender, e) => PlayMusicEvent(sender, e, musicNote);

这篇关于传递参数事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 18:01