'Xamarin Forms - Shell navigation is stuck
In my app, I've got a logout system. After logging out, the user is meant to be taken back to a splash page. However on iOS, this never happens. The user is logged out accordingly, but the navigation never happens. On Android, the logging out works for the 1st time, but then logging in and trying to log out again, makes the app not navigate to the Logout Page at all, meaning the command for OnAppearing doesn't fire. Anyone know if this is a bug, or a way to work around?
This is the function which is fired whenever the logout page appears.
private void PageAppearing()
{
_ = Task.Factory.StartNew(() => Thread.Sleep(500)).ContinueWith(async (t) =>
{
try
{
if (Thread.CurrentPrincipal.Identity is ClaimsIdentity claimsIdentity)
{
claimsIdentity.TryRemoveClaim(claimsIdentity.Claims.FirstOrDefault(c => c.Type == ClaimsConstants.GetClaimUrl(ClaimsConstants.ClaimAccessToken)));
claimsIdentity.TryRemoveClaim(claimsIdentity.Claims.FirstOrDefault(c => c.Type == ClaimsConstants.GetClaimUrl(ClaimsConstants.ClaimAccessTokenExpiresAt)));
}
Preferences.Remove("AccessToken");
Preferences.Remove("TokenExpiry");
_liveEventsService.StopLiveEventsAsync();
if (Application.Current.MainPage is AppShell appShell)
{
await appShell.GoToAsync("//WelcomePage");
}
}
catch (Exception ex)
{
Logger.Error(ex, $"Failed to logout: {ex.Message}");
}
}, TaskScheduler.FromCurrentSynchronizationContext());
}
My App.xaml
<TabBar>
<ShellContent Route="HomePage" Title="{x:Static resources:CommonResource.Dashboard}" ContentTemplate="{DataTemplate local:HomePage}">
<ShellContent.Icon>
<FontImageSource FontFamily="FontAwesome" Glyph="{x:Static fontAwesome:FontAwesomeIcons.Home}"/>
</ShellContent.Icon>
</ShellContent>
<ShellContent Route="VisualVerificationPage" Title="{x:Static dashboard:DashboardResource.VisualVerification}" ContentTemplate="{DataTemplate local:VisualVerificationPage}">
<ShellContent.Icon>
<FontImageSource FontFamily="FontAwesome" Glyph="{x:Static fontAwesome:FontAwesomeIcons.IdCardO}"/>
</ShellContent.Icon>
</ShellContent>
<ShellContent Route="PeoplePage" Title="{x:Static resources:CommonResource.People}" ContentTemplate="{DataTemplate people:PeoplePage}">
<ShellContent.Icon>
<FontImageSource FontFamily="FontAwesome" Glyph="{x:Static fontAwesome:FontAwesomeIcons.User}"/>
</ShellContent.Icon>
</ShellContent>
<ShellContent Route="DoorsPage" Title="{x:Static resources:CommonResource.Doors}" ContentTemplate="{DataTemplate local:DoorsPage}">
<ShellContent.Icon>
<FontImageSource FontFamily="tdsi" Glyph="{x:Static tdsi:TdsiIcons.Door}"/>
</ShellContent.Icon>
</ShellContent>
<ShellContent Route="LiveEventsPage" Title="{x:Static resources:CommonResource.LiveEvents}" ContentTemplate="{DataTemplate local:LiveEventsPage}">
<ShellContent.Icon>
<FontImageSource FontFamily="FontAwesome" Glyph="{x:Static fontAwesome:FontAwesomeIcons.Home}"/>
</ShellContent.Icon>
</ShellContent>
<ShellContent Route="LogoutPage" Title="{x:Static resources:CommonResource.Logout}" ContentTemplate="{DataTemplate local:LogoutPage}">
<ShellContent.Icon>
<FontImageSource FontFamily="FontAwesome" Glyph="{x:Static fontAwesome:FontAwesomeIcons.SignOut}"/>
</ShellContent.Icon>
</ShellContent>
</TabBar>
<ShellItem Route="Login">
<ShellContent ContentTemplate="{DataTemplate local:LoginPage}" />
</ShellItem>
<ShellItem Route="Welcome">
<ShellContent ContentTemplate="{DataTemplate local:WelcomePage}" />
</ShellItem>
Solution 1:[1]
I've changed the line in the function to:
Shell.Current.Navigation.PushModalAsync(new WelcomePage());
And it seems to have fixed my issue. I think this was happening becasue there was no welcome page in the Logout page navigation stack, so the app couldn't navigate to it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | vlad radoi |
