英文:
How Unsubscribe from Lambda Event
问题
如何取消订阅此 Lambda 事件?
Button1Clicked += () => Results(Result.Nothing);
这种方式不起作用:
Button1Clicked -= () => Results(Result.Nothing);
谢谢大家。
或者也可以这样做:
Button1Clicked += DoSomething;
Button1Clicked -= DoSomething;
private void DoSomething()
{
// 做一些事情
}
但是如果要传递参数,可以像这样:
Button1Clicked += () => DoSomething(Result.Nothing);
private void DoSomething(Result result)
{
// 做一些事情
}
谢谢大家。
英文:
How can i unsubscribe from this lamda event?
Button1Clicked += () => Results(Result.Nothing);
This Doesn't work:
Button1Clicked -= () => Results(Result.Nothing);
thank you all
alternatively it is possible to do :
Button1Clicked += DoSomething;
Button1Clicked -= DoSomething;
private void DoSomething()
{
do something
}
but passing a parameter, something like:
Button1Clicked += DoSomething(Result.Nothing)
private void DoSomething( Result.Nothing)
{
do something
}
Thank you all
答案1
得分: 2
来自微软文档关于事件订阅和取消订阅的内容:
如果您使用匿名函数订阅事件,那么取消订阅将不容易。要在这种情况下取消订阅,请返回到您订阅事件的代码,将匿名函数存储在委托变量中,然后将委托添加到事件中。如果您以后需要取消订阅事件,我们建议您不要使用匿名函数来订阅事件。
英文:
From Microsofts Documentation on Event Subscribing and Unsubscribing.
>You cannot easily unsubscribe from an event if you used an anonymous function to subscribe to it. To unsubscribe in this scenario, go back to the code where you subscribe to the event, store the anonymous function in a delegate variable, and then add the delegate to the event. We recommend that you don't use anonymous functions to subscribe to events if you have to unsubscribe from the event at some later point in your code.
答案2
得分: 0
正如其他答案所指出的,你不能轻松地取消订阅匿名函数。
相反,你可以创建一个带有额外参数的事件处理程序
public Form1()
{
InitializeComponent();
button1.Click += delegate(object sender, EventArgs e) { button_Click(sender, e, "这是来自Button1的消息"); };
}
void button_Click(object sender, EventArgs e, string message)
{
// 做一些操作
}
英文:
As the other answers point out, you cannot unsubscribe easily from an anonymous function.
Instead, you can create an event handler with extra parameters
public Form1()
{
InitializeComponent();
button1.Click += delegate(object sender, EventArgs e) { button_Click(sender, e, "This is From Button1"); };
}
void button_Click(object sender, EventArgs e, string message)
{
// do something
}
答案3
得分: 0
使用本地函数,它们可以取消订阅并捕获变量。
using System;
public class Program
{
public static void Main()
{
var toCapture = "Hello World";
var foo = new Foo();
foo.Bar += OnBar;
void OnBar(object sender, EventArgs args)
{
Console.WriteLine(toCapture);
}
foo.DoBar();
foo.Bar -= OnBar;
}
}
class Foo
{
public EventHandler Bar;
public void DoBar() => Bar.Invoke(this, EventArgs.Empty);
}
英文:
Use Local Functions they can be unsubscribed and capture variables.
using System;
public class Program
{
public static void Main()
{
var toCapture = "Hello World";
var foo = new Foo();
foo.Bar += OnBar;
void OnBar(object sender, EventArgs args)
{
Console.WriteLine(toCapture);
}
foo.DoBar();
foo.Bar -= OnBar;
}
}
class Foo
{
public EventHandler Bar;
public void DoBar() => Bar.Invoke(this, EventArgs.Empty);
}
eg.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论