Forum Xamarin.Forms

We need a way for ContentView and ViewCells to monitor their own lifecycle

JohanOJohanO SEMember ✭✭
edited April 2016 in Xamarin.Forms

This is aimed at the Xamarin team :)

Are you guys planning to add some lifecycle overrides / events to the ViewCell and ContentView classes? Sometimes I have special cases where I need to attach event handlers etc in the code behind of a view. The problem comes however when you need to clean these up. Unlike the Page class no OnAppearing and OnDisappearing methods exists (well they do for the ViewCell but they are fired when an item is shown/hidden rather than created/destroyed), so I have no easy way of knowing when to unhook and clean up references from my views.

My workaround for now is that I will find the controls parent page by iterating through it's parent property. I then hook up to the Appearing and Disappearing events of the page. When the disappearing event fires I will perform my cleanup. This works ok but I am also missing a "OnViewLoaded" method to override, so it's hard to know when I can start looking for my parent Page. Sometimes I can do this in the OnBindingContext changed method but not always. My safest bet so far has been to perform the search in one of the OnMeasure/OnSize methods but this feels far from optimal as they can get called multiple times.

Any insight into this? Are you planning to expand these classes with more overrides so we can perform cleanup in a more reliable/easier way?



  • jonathannicholsjonathannichols USMember ✭✭

    Is there any update on this or helpful workaround? I have an urgent need to detect after UI has appeared in ContentView

Sign In or Register to comment.