开发者

C# how to remove delegation [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Unsubscribe anonymous method in C#

I have some Panels that when created they are delegated to become clickable:

int z2 = 开发者_运维知识库z;
PicBx[z].Click += delegate { clicked(z2, null); };

I want to be able to remove this if the program calls for it. I tried using:

int z2 = z;
PicBx[z].Click -= delegate { clicked(z2, null); };

But it didn't work. Is there any way to remove the clickable delegation from it?


You can't unsubscribe from anonymous delegate, you need to keep reference to delegate

var myClickDelegate = (RoutedEventHandler)delegate { clicked(z2, null); };

PicBx[z].Click += myClickDelegate;

...

PicBx[z].Click -= myClickDelegate;

Or create named function

Hope this helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜