简体   繁体   English

如何导航到主页..net maui?

[英]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:当我想导航到另一个视图时,视图 Model 中的代码有效,但是当我想导航到主页时,我收到错误消息:

System.Exception:Relative routing to shell is currently not supported. System.Exception:目前不支持到 shell 的相对路由。 Try prefixing your uri with///:///MainPage`.尝试在您的 uri 前加上///:///MainPage`。

MainMenuViewModel:主菜单视图模型:

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

AppShell.xaml.cs: 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: 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 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;
    }
}

This code works:此代码有效:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM