Home > Blockchain >  Xamarin Shell: Redirect to different page and remove current page from nav stack
Xamarin Shell: Redirect to different page and remove current page from nav stack

Time:05-16

I imagine this is pretty straightforward, but I'm struggling to make sense of how to do it via the documentation...

I've got a tab page, that is opened from a flyout item. The tabs for this page are dynamically loaded from saved data in the code behind, and all works exactly as expected.

However, in the scenario when there is no saved data (and thus, no tabs) I want to redirect the user to a different page, and crucially, not be able to navigate back to this tab page (but allow going back to the page they were initially on BEFORE they navigated to the tabbed page)

I've got a method in initalize for the tabbed page that, if it doesn't have any saved data, attempts to do this. I started with:

Shell.Current.Navigation.PopAsync();
Shell.Current.GoToAsync(nameof(AddNewDataPage));

which navigated to the add page, but pressing the back button just resulted in the add page being shown again, over and over.

I then tried:

Shell.Current.Navigation.PopToRootAsync();
Shell.Current.GoToAsync(nameof(AddNewDataPage));

which did the same.

So next I went with trying to navigate backwards:

Shell.Current.GoToAsync("../"   nameof(AddNewDataPage));

which showed the right page again, but now the back button doesn't work

Next, I went with trying an absolute route:

Shell.Current.GoToAsync("//HomePage/"   nameof(AddNewDataPage));

which worked... sort of. The first time the user clicks the flyout for the tabbed page, it all works great. back button takes you to the home page etc... but the second and subsequent times the user clicked the flyout, they navigate to the tabbed page and my LoadData method isn't called.

I assumed this is because the tabbed page is still loaded, so I added:

protected override void OnAppearing()
{
    LoadData();
}

Now, when the user clicks the flyout for the second and subsequent times, they navigate to the HomePage page instead of the AddNewDataPage page (an improvement, I guess?)

So, now I'm at a loss.. it seems like this should be really simple, but I can't figure it out.

CodePudding user response:

You can try this. //route The route hierarchy will be searched for the specified route, upwards from the current position. The matching page will replace the navigation stack.

https://docs.microsoft.com/nl-nl/xamarin/xamarin-forms/app-fundamentals/shell/navigation?WT.mc_id=friends-0000-jamont#relative-routes

  await Shell.Current.GoToAsync($"//{nameof(AddNewDataPage)}");

https://www.youtube.com/watch?v=ylbgWHB_gMI&t=703s&ab_channel=JamesMontemagno

An explanation about relative routes.

CodePudding user response:

May I don't understand your problem clearly. But I created a simple with the flyout shell app.

At first, it seems that the construction method of the content page will jut call once which the page shows first time. So I put the following code into it. Such as:

public ItemsPage()
    {
        InitializeComponent();
        BindingContext = _viewModel = new ItemsViewModel();
        Shell.Current.Navigation.PopAsync();
        Shell.Current.GoToAsync(nameof(NewItemPage));
    }

And then when the app got into the ItemsPage first time, it will get into the NewItemPage. User can add new item here. After that, when I clicked the back button, I will get into the ItemsPage back without it's construction method called. So you can't add the LoadData(); in it.

You can put it into the OnAppearing method. Such as:

 protected override void OnAppearing()
    {
        //LoadData();
        if (_viewModel.Items.Count < 8)
        {
           Shell.Current.Navigation.PopAsync();
           Shell.Current.GoToAsync(nameof(NewItemPage));
           // then add two item in the NewItemPage
        }
        else {
           base.OnAppearing();
           _viewModel.OnAppearing();
        }
    }

This will get the same effect as above. I also try to move the Shell.Current.Navigation.PopAsync();. The result is same. It will get back to the ItemsPage when you clicked the back button in the NewItemPage after you add two item.

which navigated to the add page, but pressing the back button just resulted in the add page being shown again, over and over.

You may just need a if() to judge the condition the app need to go to the AddNewDataPage with skipping the tab page.

  • Related