简体   繁体   中英

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
    }
}

Using the NavigationStack you can view the Page s 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM