aytee77 aytee77 - 1 month ago 20
C# Question

OnAppearing check from which Page the User is comming in Xamarin.forms

Is there a way i can detect, from which page the user is coming inside of the OnAppearing event in a ContentPage? As well as by pushing a Page to the Navigation or poping it.

I'm trying to do something like this.

protected override void OnAppearing()
{
base.OnAppearing();
if(/*User comes from certain page*/){
//Do stuff
}else{
//Do other stuff
}
}

Answer

Using the NavigationStack you can view the Pages that have been pushed into the stack.

content.Appearing +=  (sender, e) =>
{
    var pages = Application.Current.MainPage.Navigation.NavigationStack;
    foreach (var page in pages)
    {
        System.Diagnostics.Debug.WriteLine(page.Title);
    }
};

Thus, you just have to look at the second to last page to determine where you are coming from....

Something like:

Application.Current.MainPage.Navigation.NavigationStack.Reverse().Take(2).Last();