0

Hi i have a dynamically created button on click which will download a video in windows universal app, while creation of button i am assigning on event handler like this:

 videoIcon.Click += (s, ev) => { Download_Video(s, ev, SomeStringParameter1, SomeStringParameter2); };

Once user clicks on button, in Download_Video, I am removing the event handler to download the video, like this:

 Button videoIcon = sender as Button;
 videoIcon.Click -= (s, ev) => { Download_Video(s, ev, videoUrl, messageId); };

and the assigning a new event handler to play video on click of same button like this:

videoIcon.Click += (s, ev) => { Video_Click(s, ev, savedFile.Name); };

The problem is previously assigned handler Download_Video also fires along with Video_Click. How to stop this?

Saurabh Sashank
  • 280
  • 1
  • 5
  • 18
  • possible duplicate of [Why can't I unsubscribe from an Event Using a Lambda Expression?](http://stackoverflow.com/questions/25563518/why-cant-i-unsubscribe-from-an-event-using-a-lambda-expression) – chue x Aug 07 '15 at 15:22

1 Answers1

2

As far as I know, this has nothing to do with Windows 10. You simply cannot unsubscribe from an anonymous event handler, as this question states.

Instead, simply keep a reference to the delegate:

RoutedEventHandler handler = (s, ev) => { Download_Video(s, ev, videoUrl, messageId); };
videoIcon.Click += handler;
videoIcon.Click -= handler;
Community
  • 1
  • 1
andreask
  • 4,248
  • 1
  • 20
  • 25
  • Thanks @andreask for pointing out "You simply cannot unsubscribe from an anonymous event handler". I am new to windows development so didn't knew this. – Saurabh Sashank Aug 07 '15 at 07:19