wojtek
wojtek

Reputation: 99

How to navigate to main page . net maui?

I want to navigate to main page, but this code doesn't work. The code in View Model works when I want to navigate to another view, but when I want to navigate to main page I get error:

System.Exception:Relative routing to shell is currently not supported. Try prefixing your uri with///:///MainPage`.

MainMenuViewModel:

[RelayCommand]
public async void GoToMenuVM()
{
    await Shell.Current.GoToAsync(nameof(MainPage));
}

AppShell.xaml.cs:

public partial class AppShell : Shell
{
    public AppShell()
    {
        InitializeComponent();
 
        Routing.RegisterRoute(nameof(ClassicModeView), typeof(ClassicModeView));        
        Routing.RegisterRoute(nameof(ChangeColorModeView), typeof(ChangeColorModeView));         
        Routing.RegisterRoute(nameof(MainPage), typeof(MainPage));
    }
}

MauiProgram.cs:

public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        builder
            .UseMauiApp<App>(); 
  
        builder.Services.AddSingleton<MainPage>();            
        builder.Services.AddSingleton<MainMenuViewModel>();                
        builder.Services.AddTransient<ClassicModeView>();            
        builder.Services.AddTransient<ClassicModeViewModel>();                
        builder.Services.AddTransient<ChangeColorModeView>();            
        builder.Services.AddTransient<ChangeColorModeViewModel>();
    
        return builder.Build();
    }
}

AppShell.xaml

<?xml version="1.0" encoding="UTF-8" ?>
<Shell
    x:Class="Clicer_Game.AppShell"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:Clicer_Game.Views"
    Shell.FlyoutBehavior="Disabled">
    
    <ShellContent
        Title="Home"
        ContentTemplate="{DataTemplate local:MainPage}"
        Route="MainPage"/>
    
</Shell>

This works

[RelayCommand]
async void MenuListViewSelectedItem(MainMenuModel e)
{
    switch (e.Name)
    {
        case "Classic":
            await Shell.Current.GoToAsync(nameof(ClassicModeView));
            break;

        case "Race light":
            await Shell.Current.GoToAsync(nameof(ChangeColorModeView)); 
            break;
    }
}

Upvotes: 1

Views: 2583

Answers (1)

wojtek
wojtek

Reputation: 99

This code works:

[RelayCommand]
public async void GoToMenuVM()
{
    await Shell.Current.GoToAsync("../../route");
}

Upvotes: 1

Related Questions