I am unable to access the value of CurrentPage in the Navigated event of the Xamarin.Forms.Shell.
I have created my own subclass of Shell that contains a top-level tab with two views and then a third screen that is
intended to be accessed modally from either of the two top-level screens. The set-up code is something like this:
public partial class MyNavigation : Shell
{
public MyNavigation()
{
Items.Add( new TabBar()
{
Items =
{
new ShellContent
{
Icon = ImageSource.FromResource( _HomeIcon ),
ContentTemplate = new DataTemplate( typeof( HomeView ) ),
Route = "Home",
},
new ShellContent
{
Icon = ImageSource.FromResource( _SettingsIcon ),
ContentTemplate = new DataTemplate( typeof( SettingsView ) ),
Route = "Settings",
}
}
});
Routing.RegisterRoute( "ModalView", typeof( ModalView ) );
Navigating += MyNavigation_Navigating;
Navigated += MyNavigation_Navigated;
}
public async Task NavigateAsync( string screenName ) => await GoToAsync( screenName );
private void MyNavigation_Navigating( object sender, ShellNavigatingEventArgs e )
{
// This is okay.
ContentPage viewBase = (ContentPage)CurrentPage;
}
private void MyNavigation_Navigated( object sender, ShellNavigatingEventArgs e )
{
// This blows up. Even the debugger cannot access the value, although it appears to have the correct type.
ContentPage viewBase = (ContentPage)CurrentPage;
}
}
public partial class App : Application
{
public App()
{
InitializeComponent();
MainPage = new MyNavigation();
}
}
public class HomeView : ContentPage
{
public HomeView()
{
Title = "Home Tab";
}
}
public class SettingsView : ContentPage
{
public SettingsView()
{
Title = "Settings Tab";
}
}
public class ModalView : ContentPage
{
public ModalView()
{
Title = "Modal View";
Shell.SetPresentationMode( this, PresentationMode.Modal );
}
}
I can navigate without error between the two screens in the tab bar. However, when I navigate to the modal screen, I am
unable to access the value of CurrentPage in the Shell Navigated event. It even crashes the debugger.
If I remove the call to SetPresentationMode(), the error doesn't happen.
I am assuming this is a bug and I have raised it with the Xamarin team but they seem to be strangely quiet about it. (I have
heard nothing for over two weeks; guess everyone is now over on MAUI.)
Does anyone know if this really is a bug? If so, is it likely to be fixed or is there something fundamentally wrong with what
I am trying to do? The Shell class is advertised as being able to do pretty much any navigation you want so I will be disappointed to say the least if I cannot do this.
Can anyone think of a workaround that will allow me to access the current page inside the Shell Navigated event?