PopAsync twice

EricPolereckyEricPolerecky ✭✭USMember ✭✭

In my App I create a MasterDetailPage, If a user is not authenticated I push a NavigationPage and a ContentPage onto the stack. The contact page has 2 links (one signup one login) that get pushed onto the stack.

When the user logs in I'd like to pop all the way back to my MasterDetailPage.

It feels like PopToRootAsync is where I want to be but it is not supported on iOS.

Posts

  • StephaneDelcroixStephaneDelcroix ✭✭✭✭ USInsider, Beta ✭✭✭✭

    on iOS PopToRootAsync is supported on the NavigationPage, but not on the NavigationProxy.

  • EricPolereckyEricPolerecky ✭✭ USMember ✭✭

    Are you saying that I should keep passing down my NavigationPage into it's children?

    Or do I can't this.Parent as NavigationPage?

  • StephaneDelcroixStephaneDelcroix ✭✭✭✭ USInsider, Beta ✭✭✭✭

    @EricPolerecky‌ do it the way you prefer, either by passing the NavigationPage or walking the Parent tree up.

  • JflyJfly USMember

    What do you mean walking the Parent tree up? Or use this.Parent as NavigationPage? Can you give a code example?

  • StephaneDelcroixStephaneDelcroix ✭✭✭✭ USInsider, Beta ✭✭✭✭

    this.Parent won't always be the NavigationPage, unless you've set it that way.

    this is what I meant:

    var control = this;
    NavigationPage navpage = null;
    while (navpage == null && control.Parent!=null) {
        control = control.Parent;
        navpage = control as NavigationPage;
    }
    
  • ScottBradleyScottBradley ✭✭ AUMember ✭✭

    @StephaneDelcroix‌ is this what you mean? This is in my TabbedPage

    MyPage p1 = new MyPage ();
    NavigationPage n1 = new NavigationPage (p1);
    p1.Parent = n1;
    Children.Add (n1);
    

    I'm getting a null on the third page I have pushed so I suppose I also need to pass this Parent on to each page I push onto the stack.

  • ScottBradleyScottBradley ✭✭ AUMember ✭✭

    So I added your function to my BasePage & BaseView and it was all looking good until my last page was a Modal which it didn't like.

    Is there any benefits to using Parent and not your own property?

  • HugoLogmans_HugoLogmans_ ✭✭✭ NLMember ✭✭✭

    This PopToRootAsync on iOS bugged me, too. Somehow it feels wrong to manage this navigation stack yourself and passing objects around. I would definitely go for traversing the parent property, might be reusable as an extension method.

  • ScottBradleyScottBradley ✭✭ AUMember ✭✭

    This is close...

    I put this on my BaseView

        public BasePage BasePage{
            get{
                Element control = this;
                BasePage basePage = null;
                while (basePage == null && control.Parent != null) {
                    control = control.Parent;
                    basePage = control as BasePage;
                }
                return basePage;
            }
        }
    

    and this on my BasePage

        private NavigationPage navigationPage;
    
        public NavigationPage NavigationPage {
            get{ return navigationPage; }
            set{ navigationPage = value; }
        }
    

    .... but control on the BaseView is derived from BasePage so it ends up being null :(

    I also unsuccessfully tried to match the control & BasePage with something like this.

        public bool IsSameOrSubclass(Type potentialBase, Type potentialDescendant)
        {
            return potentialDescendant.IsAssignableFrom(potentialBase)
                || potentialDescendant == potentialBase;
        }
    

    You would think being able to tell if a class is a descendant of another one would be easy to find out right!

  • ScottBradleyScottBradley ✭✭ AUMember ✭✭

    Ok I lied this did work. My page wasn't inheriting from BasePage ... again!

    No need for the IsSameOrSubclass function

Sign In or Register to comment.