请查看以下代码
SpeechRecognizer sr2 = new SpeechRecognizer();
...
sr2.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(sr2_SpeechRecognized);
...
void sr2_SpeechRecognized(object sender, SpeechRecognizedEventArgs e){}
在这里,第一个代码显示语音识别器的初始化,第二个代码显示注册到事件处理程序,第三个代码显示事件处理程序。
现在,我需要删除此事件处理程序。我怎样才能做到这一点?请帮忙..
答案 0 :(得分:3)
只需使用
sr2.SpeechRecognized -= new EventHandler<SpeechRecognizedEventArgs>(sr2_SpeechRecognized);
由于Remove
方法使用Delegate.Equals
来检查是否相等,因此您无需在任何地方实际存储new EventHandler<SpeechRecognizedEventArgs>(sr2_SpeechRecognized);
,并且可以简单地进行上述调用以删除处理程序。