Search code examples
c#.netnavigationmaui

MAUI: Relative routing to shell elements is currently not supported


I recreated a new app maui and to make the navigation I redid what worked on another app that I made (on the other app it still works). As soon as I navigate on a new page everything goes well but it's as soon as I want to come back on the page where I was or I have a bug.

NavigationService.cs

public async Task NavigateAsync(NavigationPages page)
        {

            try
            {
                await Shell.Current.GoToAsync(page.ToString(), true);
            }
            catch (Exception ex)
            {

            }

        }

ViewModel.cs

public ICommand GoBacKCommand { get; }

public ParamViewModel(INavigationService navigationService)
        {
            GoBacKCommand = new Command(GoBacKExecute);
            this.navigationService = navigationService;
        }
private void GoBacKExecute()
        {
            navigationService.NavigateAsync(BusinessModels.NavigationPages.MainPage);
        }

MauiProgram.cs

        Routing.RegisterRoute(nameof(MainPage), typeof(MainPage));
        services.AddTransient<MainPage>();
        services.AddTransient<MainViewModel>();

and the Exception is : Relative routing to shell elements is currently not supported. Try prefixing your uri with ///: ///MainPage

but if I do what it says (///MainPage) the navigation does not work anymore

How can I solve this problem?


Solution

  • If you want go back to a page (like PopUp) you need to need use GotoAsync as below

    ViewModel.cs

      public ICommand Back_Command { get; set; }
      public ViewModel()
      {
            Back_Command = new Command(Back);
      }
      private async void Back()
      {
            await Shell.Current.GoToAsync("..");
      }
    

    Page.xaml

     <Shell.BackButtonBehavior>
        <BackButtonBehavior 
            Command="{Binding Back_Command}"/>
     </Shell.BackButtonBehavior>
    

    Further explanation has been stated in this documentation https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/shell/navigation