I subscribe / create a custom Event Handler with the following code:
myButton.Click += (sender, e) => MyButtonClick(sender, e, stuff1, stuff2);
I want to unsubscribe / remove and tried it like this:
myButton.Click += MyButtonClick;
But throws the following error:
No overload for 'MyButtonClick' matches delegate 'System.Windows.RoutedEventHandler'
And like this:
myButton.Click += MyButtonClick(sender, e, stuff1, stuff2);
But throws the following error:
Cannot implicitly convert type 'void' to 'System.Windows.RoutedEventHandler'
How do I unsubscribe / remove that same Event Handler?
When you use Lambda you need to keep a reference of it to unsubscribe.
Try this
RoutedEventHandler handler = (sender, e) => MyButtonClick(sender, e, stuff1, stuff2);
myButton.Click += handler;//Subscribe
//Some more code
myButton.Click -= handler;//Unsubscribe
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With