Joehl Joehl - 1 month ago 47
C# Question

Page lifecycle events in xamarin.forms

I develop my first xamarin.forms app. I am excited from xamarin.forms. But I miss several events (or I did not find theme until now).

Are there som page-lifecycle-events in a ContentPage from xamarin.forms?

I know there are following events:

protected override void OnAppearing()
{
}

protected override void OnDisappearing()
{
}


But the OnAppearing() event only get fired once. When I (on android) push the start-button and go back to my app, this event is not fired again.

Is there a workaround for this (like OnNavigatedTo in WindowsPhone-pages)? Thanks.

Answer

So the OnAppearing event is fired when your page is appearing. I.e. you have navigated to that page or back to that page from another in the Stack. There is currently no Page Lifecycle events as you can see from the API documentation

I think what you are talking about is if you put your application to sleep and navigate back into it the OnAppearing event is not fired, This is because your page hasn't appeared because it was already there, the application was just asleep.

What you are looking for is the App Lifecycle which includes methods such as:

protected override void OnStart()
{
    Debug.WriteLine ("OnStart");
}
protected override void OnSleep()
{
    Debug.WriteLine ("OnSleep");
}
protected override void OnResume()
{
    Debug.WriteLine ("OnResume");
}

You can then use the OnResume event to do what you are looking for.

That Xamarin Document includes the changes that must be made to old Xamarin projects to access these events. e.g. your App class in your shared library must inherit from Xamarin.Forms.Application and changes must also be made to the AppDelegate and MainActivity.

Comments