简体   繁体   中英

Change already attached event handler runtime

I have created a TextBox dynamically and attached a Tap event handler to it using:

control.Tap += new EventHandler<System.Windows.Input.GestureEventArgs>(OnClick1);

It works fine. But, now I want to change the event handler to point to some different method. I tried:

control.Tap += new EventHandler<System.Windows.Input.GestureEventArgs>(OnClick2);

But, it still points to first event handler. ie OnClick1 . What can I do to make it point OnClick2 ? Also is there a way to remove this event handler completely?

You need to remove the first handler first:

control.Tap -= OnClick1;
control.Tap += OnClick2;

(Note the rather simpler use of method group conversions, instead of explicitly creating the event handler. It does the same thing, but is much more readable.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM