I have a simple user control with the following content:
<Grid x:Name="LayoutRoot">
<Button x:Name="btnOpenGenericPage" Click="btnOpenGenericPage_Click" Content="Open"/>
</Grid>
I understand how the click event handler is created and wired up -> on the InitializeComponent method of the .g.cs class the System.Windows.Application.LoadComponent method is called and all the magic is done.
However, once this control is removed from the VisualTree, I don't seem to understand if the unwiring of the event is ocurring. If yes, who takes care of this? Are there any situations where the event won't be unwired? Can someone please shed some light on this is开发者_StackOverflow中文版sue?
Many thanks in advance, Bruno.
It won't unwire itself. You will have to do that yourself:-
btnOpenGenericPage.click -= btnOpenGenericPage_Click
not only that but of you want to make sure that btOpenGenericPage is released you would need:-
btnOpenGenericPage = null;
Of course if the Usercontrol
on which btnOpenGenericPage
is sited is itself being removed from the tree such that it becomes elligible for garbage collection then you wouldn't normally need to do anything.
Visual Studio unwires it, by removing it from the generated code. The method may remain in the code-behind file, but it's disconnected and doesn't run.
精彩评论