Xamarin.Forms
Xamarin.Forms copied to clipboard
[Bug] NavigationStack's first page is null and page can not be extracted form shell
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.
Steps to Reproduce
- Create a Xamarin 4 shell project with two bottom bars which contain a ListView each.
- Push a new Page onto the NavigationStack.
- 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
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 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.
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()
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.
Same issue in Xamarin Forms v5.0.0
It also breaks all PopAsync as the previous item is null.
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.
It's just weird to debug and wanting to look inside the first item of shell but shell says it's null :-/
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.