locked
How to detect what page is on top? RRS feed

  • Question

  • User249511 posted

    I want to detect the type of the page on top of the stack(the current opened page) with xamarin.forms. What I want to do is preventing the user from opening the same page again; ie: he can't open page A if it's already opened, He can open it only if he only opened some page X before it. So I'm trying to get the current page type to check it with the page's type the user is trying to open.

    Saturday, November 12, 2016 11:16 AM

Answers

  • User2148 posted

    In Navigation you should have a NavigationStack property... take a look

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Saturday, November 12, 2016 11:17 AM
  • User89714 posted

    @abozanonaabozanona - For non-modal pages, I perform a test as follows

        public bool PageTypeIsAlreadyAtTopOfStack(
            ContentPage parentPage,
            Type typeofPageAppearing)
        {
                var stack = parentPage.Navigation.NavigationStack;
                return (stack[stack.Count - 1].GetType() == typeofPageAppearing);
        }
    

    You can do something very similar for modal pages as well.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Sunday, November 13, 2016 11:28 AM

All replies

  • User2148 posted

    In Navigation you should have a NavigationStack property... take a look

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Saturday, November 12, 2016 11:17 AM
  • User89714 posted

    @abozanonaabozanona - For non-modal pages, I perform a test as follows

        public bool PageTypeIsAlreadyAtTopOfStack(
            ContentPage parentPage,
            Type typeofPageAppearing)
        {
                var stack = parentPage.Navigation.NavigationStack;
                return (stack[stack.Count - 1].GetType() == typeofPageAppearing);
        }
    

    You can do something very similar for modal pages as well.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Sunday, November 13, 2016 11:28 AM
  • User272679 posted

    Dont know if this works, but try using linq; and calling stack.First();

    Sunday, November 13, 2016 10:19 PM
  • User346448 posted

    @JohnHardman What do you use for modal pages? I want to see (after app resume) if the login page is still being shown. Thanks!

    Wednesday, September 6, 2017 5:24 PM
  • User89714 posted

    @kdaveid - Modal pages have their own stack - Navigation.ModalStack instead of Navigation.NavigationStack

    Wednesday, September 6, 2017 5:33 PM
  • User346448 posted

    Thanks! Now the questions seems stupid... I've checked again the docs. I didn't see it, but it is mentioned... https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/navigation/modal/

    Wednesday, September 6, 2017 5:35 PM