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.
- if loaded occurs twice , tries add event handler again there problems?
- how should handle unregistering event? automatically handle unregistering on destruction or need handle in event unloaded or something?
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.
- if object gone cannot raise events, no need that. , handler not keep object alive (it's other way around).
Comments
Post a Comment