Sourabh Sashank
Sourabh Sashank

Reputation: 300

Removing Event Handler Not Working in Windows 10 Universal App

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?

Upvotes: 0

Views: 442

Answers (1)

andreask
andreask

Reputation: 4298

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;

Upvotes: 2

Related Questions