I am coding my user flow but this time I am going to dynamically create the menu.
So, in my shell, I have two pages.
The startup page is responsible for checking for the token and redirect the user.
public class StartupPageViewModel : BaseViewModel {
readonly IAppService _appService;
public StartupPageViewModel(IAppService appService) {
_appService = appService;
HandleNavigation(_appService);
}
private async void HandleNavigation(IAppService appService) {
var currentUserEmail = await SecureStorage.GetAsync("CurrentUser");
if(string.IsNullOrEmpty(currentUserEmail)) {
await appService.NavigateTo($"//{nameof(LoginPage)}");
} else {
await appService.NavigateTo($"//{nameof(WelcomePage)}");
}
}
}
<Shell
x:Class="DemyAI.AppShell"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:badge="clr-namespace:Syncfusion.Maui.Core;assembly=Syncfusion.Maui.Core"
xmlns:helpers="clr-namespace:DemyAI.Helpers"
xmlns:mct="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
xmlns:pages="clr-namespace:DemyAI.Views"
xmlns:sfavatar="clr-namespace:Syncfusion.Maui.Core;assembly=Syncfusion.Maui.Core"
xmlns:vm="clr-namespace:DemyAI.ViewModels"
Title="DemyAI"
x:DataType="vm:AppShellViewModel"
FlyoutBehavior="{OnIdiom Desktop=Locked,
Phone=Flyout}">
<Shell.FlyoutHeader>
<Grid>
<BoxView
BackgroundColor="#522cd4"
HeightRequest="142" />
<VerticalStackLayout>
<Image
HeightRequest="80"
Source="user.png"
WidthRequest="80" />
<Label
HorizontalTextAlignment="Center"
Text="{Binding User.DemyId}"
TextColor="White" />
<Label
HorizontalTextAlignment="Center"
Text="{Binding User.Email}"
TextColor="White" />
<Label
HorizontalTextAlignment="Center"
Text="{Binding User.Role}"
TextColor="White" />
</VerticalStackLayout>
</Grid>
</Shell.FlyoutHeader>
<Shell.FlyoutFooterTemplate>
<DataTemplate>
<Button
Margin="20"
Command="{Binding SignOutCommand}"
CornerRadius="8"
Text="Log out" />
</DataTemplate>
</Shell.FlyoutFooterTemplate>
<ShellContent
ContentTemplate="{DataTemplate pages:StartupPage}"
Route="StartupPage"
Shell.FlyoutBehavior="Disabled" />
<ShellContent
ContentTemplate="{DataTemplate pages:LoginPage}"
Route="loginPage"
Shell.FlyoutBehavior="Disabled" />
</Shell>
I put all the navigation in an interface
public interface IAppService {
Task NavigateTo(string pageName, bool isAnimated, Dictionary<string, object> obj);
Task NavigateTo(string pageName);
Task DisplayToast(string message, ToastDuration toastDuration, double fontSize);
Task DisplayAlert(string title, string message, string cancelMessage);
Task NavigateBack();
}
public class AppService : IAppService {
public async Task DisplayAlert(string title, string message, string cancelMessage) {
await Shell.Current.DisplayAlert(title, message, cancelMessage);
}
public async Task DisplayToast(string message, ToastDuration toastDuration, double fontSize) {
var toast = Toast.Make(message, toastDuration, fontSize);
await toast.Show();
}
public async Task NavigateTo(string pageName, bool isAnimated, Dictionary<string, object> obj) {
await Shell.Current.GoToAsync(pageName, isAnimated, obj);
}
public async Task NavigateTo(string pageName) {
await Shell.Current.GoToAsync(pageName, true);
}
public async Task NavigateBack() {
await Shell.Current.GoToAsync("..");
}
}
But a am getting this error.
System.Exception: 'Global routes currently cannot be the only page on the stack, so absolute routing to global routes is not supported. For now, just navigate to: LoginPage'
also, I don't want my flyout the be visible in those Login page.
I am basically trying to replicate this.
https://www.youtube.com/watch?v=lSmRAV5IIBs