Forum Xamarin.Forms

Navigation Xamarin Forms

Erar2rErar2r USMember ✭✭

Hello, your help please.

I have a problem with the Navigation of Xamarin Forms. In my application I use Navigation for navigate from my ViewModels. I created my navigation stack using Navigation.PushModalAsync (new PageA (), false);
My problem comes in the following case, where I created the following navigation PageA -> PageB -> PageC -> PageD -> PageE. In the PageE I do Navigation.ModalStack.Count for Navigation.PopModalAsync (false); when the for is finished I verify that the stack of the
Navigation is empty and in effect is empty with count = 0. Then I execute the following line of code: Navigation.PushModalAsync (new PageB (), false); With what I would expect it to go to page B, however, it loads page D.

I do not understand why it shows me page D when I had removed all the pages from the stack and I am putting on the stack the page B.

Excuse my English.e

Thank you very much for the help

A greeting
Andrea

Posts

  • LandLuLandLu Member, Xamarin Team Xamurai

    Can you share a sample to declare more details about this?

  • Erar2rErar2r USMember ✭✭

    I can't post link.

    A simple example:

    In my ViewModel the PageEViewModel have:
    await Navigation.PopModalToRootAsync(); await Navigation.PushModalAsync(new PageB());

    My method PoPModalToRootAsync() have:
    public static async Task PopModalToRootAsync(this INavigation navigation) { try { if (navigation != null) { for (int i = 0; i < navigation.ModalStack.Count; i++) { await navigation.PopModalAsync(); } } } catch { while (navigation.ModalStack.Count > 0) { await navigation.PopModalAsync(); } } }

    For the Navigation between pages have:
    await Navigation.PushModalAsync(new PageB(), false); await Navigation.PushModalAsync(new PageC(), false); await Navigation.PushModalAsync(new PageD(), false); await Navigation.PushModalAsync(new PageE(), false);

    Thanks

  • SzbestaSzbesta Member ✭✭✭

    Now its readable

    public static async Task PopModalToRootAsync(this INavigation navigation) 
    { 
        try 
        { 
            if (navigation != null) 
            { 
                for (int i = 0; i < navigation.ModalStack.Count; i++) 
                { 
                    await navigation.PopModalAsync(); 
                } 
            } 
        }
        catch 
        { 
            while (navigation.ModalStack.Count > 0) 
            { 
                await navigation.PopModalAsync(); 
            } 
        } 
    }
    
Sign In or Register to comment.