如何取消订阅使用 lambda 表达式的事件? [英] How to unsubscribe from an event which uses a lambda expression?

查看:13
本文介绍了如何取消订阅使用 lambda 表达式的事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码让 GUI 响应集合中的更改.

I have the following code to let the GUI respond to a change in the collection.

myObservableCollection.CollectionChanged += ((sender, e) => UpdateMyUI());

首先,这是一个很好的方法吗?

First of all is this a good way to do this?

第二:取消订阅此事件的代码是什么?是不是一样,但使用 -=(然后又是完整的匿名方法)?

Second: what's the code to unsubscribe from this event? Is it the same but with -= (and then the complete anonymous method again)?

推荐答案

如果您需要取消订阅事件,则需要实例化引用.不幸的是,这意味着您不能使用该特定语法.

If you need to unsubscribe from an event, you need an instanced reference. Unfortunately, that means you can't use that particular syntax.

这篇关于如何取消订阅使用 lambda 表达式的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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