Adding a route to the shell dynamically and in runtime

Eduardo Gomez 3,671 Reputation points
2024-03-14T21:34:34.6566667+00:00

The app is working, I am able to create an account and navigate to select my role (teacher, student, coordinator)

when I log out, I erase everything.

 [RelayCommand]
  async Task SignOut() {
      SecureStorage.Default.RemoveAll();
      FlyoutHelper.GeetDefaultMenuItems();
      await appService.NavigateTo($"//{nameof(LoginPage)}", true);

and then I add my default pages

    public static void GeetDefaultMenuItems() {
        var defaultItems = new List<ShellContent> {
             new() { ContentTemplate = new DataTemplate(typeof(StartupPage)),
                Route = nameof(StartupPage), FlyoutItemIsVisible = false },
            new() { ContentTemplate = new DataTemplate(typeof(LoginPage)),
                Route = nameof(LoginPage), FlyoutItemIsVisible = false },
            new() { ContentTemplate = new DataTemplate(typeof(NoInternetPage)),
                Route = nameof(NoInternetPage), FlyoutItemIsVisible = false },
            new() { ContentTemplate = new DataTemplate(typeof(RoleSelectionPage)),
                Route = nameof(RoleSelectionPage), FlyoutItemIsVisible = false }
        };
        Shell.Current.Items.Clear();
        foreach(var item in defaultItems) {
            Shell.Current.Items.Add(item);
        }
    }
}


So, in my login page, I want to check if the user I already in the database, because, if it is, that means that he has a role assigned to it

 async Task Login() {

     IsBusy = true;
     var user = await authenticationService.LoginWithEmailAndPassword(User.Email!, User.Password!);

     if(user is not null) {
         var loggedUser = await authenticationService.GetLoggedInUser();
         var currentUser = await dataService.GetByEmailAsync<DemyUser(Constants.USERS,
         loggedUser!.Info.Email);

         if(currentUser is not null) {

             ShellItem shellItem = new() { Route = $"{currentUser.CurrentRole}DashboardPage" };

             Shell.Current.Items.Add(shellItem);
             await appService.NavigateTo($"//{currentUser.CurrentRole}DashboardPage", true);
         }
         await appService.NavigateTo($"//{nameof(RoleSelectionPage)}", true);]


But I get {"unable to figure out route for: //TeacherDashboardPage (Parameter 'uri')"}

Demo:

https://reccloud.com/u/d1at3wo

Developer technologies | .NET | .NET MAUI
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 36,436 Reputation points Microsoft External Staff
    2024-03-18T07:06:31.7566667+00:00

    Hello,

    You could try to set a new shell for the MainpPage instead of removing all menu items, and you could try adding new pages again for the new shell when you sign out the account and login again.

    Best Regards,

    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.