c# - When to remove event handlers from an object? -


so registering event handlers in object's loaded event.

tv.previewmousedown += new mousebuttoneventhandler(signalscrollviewer_previewmousedown); 

but had 2 questions.

  1. if loaded occurs twice , tries add event handler again there problems?
  2. how should handle unregistering event? automatically handle unregistering on destruction or need handle in event unloaded or something?

  1. yes cause subscription causes handler execute twice. can remove loaded handler inside loaded handler.

    msdn:

    loaded , unloaded might both raised on controls result of user-initiated system theme changes. theme change causes invalidation of control template , contained visual tree, in turn causes entire control unload , reload. therefore loaded cannot assumed occur when page first loaded through navigation page.

  2. if object gone cannot raise events, no need that. , handler not keep object alive (it's other way around).

Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -