Xamarin.Forms icon indicating copy to clipboard operation
Xamarin.Forms copied to clipboard

[Bug] NavigationStack's first page is null and page can not be extracted form shell

Open shyckwave opened this issue 5 years ago • 8 comments

Description

We would like to extract the currently active view out of the shell.

The exact situation is that we have two bottom tabs which navigate to different views. Both views contain a ListView, which execute PushAsync on selecting an item. When calling PopAsync we previously used OnAppearing for reloading the data, but that is not being called anymore.

Instead we found out that, on every type of navigation, OnNavigated(ShellNavigatedEventArgs) in the shell is being called. We tried to extract the active view out of Shell.Current, but didn't manage to do so.

NavigationProblemSample.zip

Steps to Reproduce

  1. Create a Xamarin 4 shell project with two bottom bars which contain a ListView each.
  2. Push a new Page onto the NavigationStack.
  3. Pop the page.

Expected Behavior

The first page on the NavigationStack contains the page navigated to by the bottom tab bar. BaseShellItem.Page/ShellSection.PresentedPage property should be visible to extract the active view.

Actual Behavior

The first page on the NavigationStack is null after navigating via the bottom tab bar. In Addition, BaseShellItem.Page/ShellSection.PresentedPage should be visible properties to extract the active view.

Basic Information

  • Version with issue: 4.0.0.540366
  • Last known good version: -
  • IDE: Visual Studio 2019 16.1.3
  • Platform Target Frameworks:
    • Android: 9.0
  • Android Support Library Version: 28.0.0.1
  • Nuget Packages: Xamarin.Forms, Xamarin.Essentials

shyckwave avatar Jun 27 '19 09:06 shyckwave

While this is obviously a bug and needs to be fixed, temporary workaround to get first page is: ((IShellContentController) AppShell.CurrentItem.CurrentItem.CurrentItem).Page

viktorszekeress avatar Jan 12 '20 11:01 viktorszekeress

@viktorszekeress the suggestion you provided does work to get the first item, but does not work when you attempt to remove the first page.

If you call Shell.Current.Navigation.RemovePage(firstPage) the call will execute, but the navigation stack will not remove the first item.

ckrempp91 avatar Feb 27 '20 15:02 ckrempp91

I am facing same issue. I wanna get the first page every time, so I can InserPageBefore() my error page so then I should PopToRootAsync() and show fatal error page. But with this bug, when I get the first page from the stack it is null so I can not do InsertPageBefore()

RomanPoschl avatar Mar 31 '20 13:03 RomanPoschl

Same issue.

  • Version with issue: 4.7.0.1080
  • Last known good version: -
  • IDE: Visual Studio 2019 16.6.3
  • Platform Target Frameworks:
    • Android: 9.0
  • Android Support Library Version: 28.0.0.3
  • Nuget Packages: Xamarin.Forms, Xamarin.Essentials

Because of this bug it is impossible to structure a Login cycle going then to insert the root page of the application after Login.

FedericoNembrini avatar Jul 14 '20 21:07 FedericoNembrini

Same issue in Xamarin Forms v5.0.0

FranRDev avatar Mar 04 '21 07:03 FranRDev

It also breaks all PopAsync as the previous item is null.

MhAllan avatar Jul 25 '21 15:07 MhAllan

Inserting your page isn't really the correct way to think of shell. If you're doing a login flow then setup a separate set of shell flyoutitems/shellitems/tabbars

And then you can use "IsVisible" on the item to hide or show it.

PureWeen avatar Jul 25 '21 22:07 PureWeen

It's just weird to debug and wanting to look inside the first item of shell but shell says it's null :-/

MaxFelinger avatar May 04 '22 17:05 MaxFelinger

This bug is stil there - Xamarin.Forms 5.0.0.2196. My hunch is that Shell.Current.GoToAsync does not maintain the NavigationStack correctly. The framework gets confused.

I found that I can successfully remove that page with Navigation.RemovePage, however that throws a System.ArgumentNullException exception. For now I am swallowing that and NavigationStack stuff does what I expect it to do.

pdschuller avatar Oct 10 '22 22:10 pdschuller