About Navigation Modal Pages

Hi all.

How do I remove two or more Modal pages from stack ?
The count after pass for 5 pages remains is 0;

Tagged:

Answers

  • Using PushModalAsync(new page)

  • JohnHardmanJohnHardman GBUniversity mod

    @nilmarpubliogmailcom said:
    How do I remove two or more Modal pages from stack ?

    Call await Navigation.PopModalAsync() more than once.

    @nilmarpubliogmailcom said:
    The count after pass for 5 pages remains is 0;

    Not sure what you mean by that. Do you mean that the count of items in the stack is 0 after pushing 5 pages? Are you checking the right stack? (there is one stack for modal pages and one stack for non-modal pages).

  • What I mean is: Dependig on some results in process, I need go back n pages.

  • Removing them from stack. I did ie using pushModal but I know that is a wrong solution.

  • JohnHardmanJohnHardman GBUniversity mod

    @nilmarpubliogmailcom said:
    What I mean is: Dependig on some results in process, I need go back n pages.

    @JohnHardman said:
    Call await Navigation.PopModalAsync() more than once.

    If you are using PushModalAsync to push pages, to get rid of them you call PopModalAsync.

  • PopModalAsync only returns one page. I need to return for example from the fifth page to the third depending on conditions, or from the last page to the first page of the stack.
    Was it clear?

  • JohnHardmanJohnHardman GBUniversity mod
    edited October 8

    @nilmarpubliogmailcom said:
    PopModalAsync only returns one page. I need to return for example from the fifth page to the third depending on conditions, or from the last page to the first page of the stack.
    Was it clear?

    @JohnHardman said:
    Call await Navigation.PopModalAsync() more than once.

    Hence the more than once

  • Hi John, unfortunately it didn't work.
    putting 4 await Navigation.PopModalAsync (); sequentially it returns only one page

  • Any other suggestions?

  • JohnHardmanJohnHardman GBUniversity mod
    edited October 8

    @nilmarpubliogmailcom said:
    Hi John, unfortunately it didn't work.
    putting 4 await Navigation.PopModalAsync (); sequentially it returns only one page

    It depends where/how you do it. Don't do it in a tight loop in the scope of one of the pages being popped.

  • Jo John,

    I'm doing this:

    Page 1
         Page2
             Page 3
                 Page 4
                     Page 5
                         Page 6

    Suppose I'm on page 6 and want to go back to page 3, I did it:

    await PopModalAsync ();
    await PopModalAsync ();
    await PopModalAsync ();

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    @nilmarpubliogmailcom Do you want to achieve the result like following GIF?

    In the last page, you can call the Navigation.PopToRootAsync(); in the last page, then push all of the pages to the first page. If you want to use Navigation.PopToRootAsync(); , you should init MainPage =new NavigationPage( new MainPage()); in the App.xaml.cs, when you navigation page, you should use Navigation.PushAsync(new Page1());

  • JohnHardmanJohnHardman GBUniversity mod
    edited October 8

    @LeonLu - The OP is asking about modal pages

  • Thanks

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    If you solve this issue, please mark the JohnHardman's reply, it will help others who have similar issue.

  • batmacibatmaci DEMember ✭✭✭✭✭
    You should use remove page fro page 4 and 5 and do a pop page on page 6 because you cannot remove current page you should pop it. Thus you will end up on page 3.

    https://docs.microsoft.com/en-us/dotnet/api/xamarin.forms.inavigation.removepage?view=xamarin-forms
  • batmacibatmaci DEMember ✭✭✭✭✭
    You have to pay attention using modalstack instead of navigation stack. If I remember correctly in older versions modal pages were in modalstack instead of navigation stack. Even I remember reporting a bug for this issue. I dont know what is the current status in the latest version of xamarin. You need play with both and see which one is correct
  • JohnHardmanJohnHardman GBUniversity mod
    edited October 10

    @nilmarpubliogmailcom

    @JohnHardman said:
    It depends where/how you do it. Don't do it in a tight loop in the scope of one of the pages being popped.

    Have a play with the following, by calling

    await Navigation.PushModalAsync(new NestedModalPageView(1));
    
        public class NestedModalPageView : ContentPage
        {
            private static int _closeCount;
            protected async override void OnAppearing()
            {
                base.OnAppearing();
    
                if (_closeCount > 1)
                {
                    --_closeCount;
                    await Navigation.PopModalAsync();
                }
            }
    
            public NestedModalPageView(int index)
            {
                Title = $"Index {index}";
                Content = new StackLayout
                {
                    VerticalOptions = LayoutOptions.FillAndExpand,
                    Children =
                    {
                        new Label
                        {
                            Text = $"Index {index}",
                            FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label)),
                            BackgroundColor = Color.Red,
                            TextColor = Color.Yellow,
                        },
                        new Button
                        {
                            Text = "Push another page",
                            Command = new Command(async () =>
                            {
                                await Navigation.PushModalAsync(new NestedModalPageView(index+1));
                            })
                        },
                        new Button
                        {
                            Text = "Pop 1 page",
                            Command = new Command(async() =>
                            {
                                await Navigation.PopModalAsync();
                            })
                        },
                        new Button
                        {
                            Text = "Pop 3 pages",
                            IsVisible = (index >= 3),
                            IsEnabled = (index >= 3),
                            Command = new Command(async() =>
                            {
                                _closeCount = 3;
                                await Navigation.PopModalAsync();
                            })
                        }
                    }
                };
            }
        }
    

    Once you've seen/understood how to do it, I'd suggest considering whether your use of modal pages is really the design that you want to follow. Just because it can be done, doesn't mean it's a good thing to do...

  • JohnHardmanJohnHardman GBUniversity mod

    @nilmarpubliogmailcom

    Did the code above answer your question?

Sign In or Register to comment.