1
votes

I'm using Xamarin Forms and I'm having an issue use InsertPageBefore() method with existing objects of Pages.

Here is my view code:

private FirstPage firstPage; 
private SecondPage secondPage = new SecondPage();
private ThirdPage thirdPage = new ThirdPage(); 
private async void ItemSelectedMethod()
{
        var root = App.NavigationPage.Navigation.NavigationStack[0];
        if (SelectedItem == Items[0])
        {
            if (!IsFirstChoose)
            {
                App.NavigationPage.Navigation.InsertPageBefore(firstPage, root);
                await App.NavigationPage.PopToRootAsync(false);
            }
        }
        if (SelectedItem == Items[1])
        {
            App.NavigationPage.Navigation.InsertPageBefore(secondPage, root);
            await App.NavigationPage.PopToRootAsync(false);
        }
        if (SelectedItem == Items[2])
        {
            App.NavigationPage.Navigation.InsertPageBefore(thirdPage, root);
            await App.NavigationPage.PopToRootAsync(false);
        }

        IsFirstChoose = false;
        rootPageViewModel.IsPresented = false;
}

It's throw exception "System.ArgumentException: 'Cannot insert page which is already in the navigation stack'". How to switch between existing objects of pages? I don't want create new object in InsertPageBefore(). I tried use it code, before call InsertPageBefore():

foreach (var item in App.NavigationPage.Navigation.NavigationStack.ToList())
                App.NavigationPage.Navigation.RemovePage(item);

But it's not working... Can anyone help me?

1
possible duplicate of stackoverflow.com/questions/44865472/… What are you trying to achieve? Why you are using Insert and Pop instead of Push?Yuri S
I want it's menu: csharp-dev.pl/wp-content/uploads/2017/04/5.png When I click on item, his page is showing as "root page".luki
looks like you are using master/detail page. Is your NavigationPage in details?Yuri S
Yes. rootPage.Detail = NavigationPage; It's code working on older version of Xamarin with Android, but don't work on new version of Xamain and on Windows 10 Mobile.luki
When I use this line: App.NavigationPage.Navigation.InsertPageBefore(new ThirdPage(), root); all is ok, but I don't want create new object in args for this method (InsertPageBefore).luki

1 Answers

1
votes

It didn't work with UWP. Here is agly workaround for you but you really need to read how to work with Master-Detail pages.

public partial class App : Application
    {
        public static RootPage RootPage { get; private set; } //DON'T DO THIS, 
                                                              //FIND A BETTER WAY 
        public App()
        {
            InitializeComponent();

            RootPage = new RootPage();
            MenuPage menuPage = new MenuPage(RootPage.vm);

            RootPage.Master = menuPage;
            RootPage.Detail = new NavigationPage(new MainPage());// NavigationPage;
            MainPage = RootPage;
        }

        protected override void OnStart()
        {
            // Handle when your app starts
        }

        protected override void OnSleep()
        {
            // Handle when your app sleeps
        }

        protected override void OnResume()
        {
            // Handle when your app resumes
        }
    }

Then

private async void ItemSelectedMethod()
        {

            if (SelectedItem == Items[0])
            {
                App.RootPage.Detail = new NavigationPage(mainPage);
            }
            if (SelectedItem == Items[1])
            {
                App.RootPage.Detail = new NavigationPage(secondPage);
            }
            if (SelectedItem == Items[2])
            {
                App.RootPage.Detail = new NavigationPage(thirdPage);
            }
            rootPageViewModel.IsPresented = false;
        }