.NET MAUI Shell导航无法重复跳转至第二页问题求助
.NET MAUI Shell导航二次跳转无响应问题
我在使用.NET MAUI的Shell导航功能时遇到了问题:主页面菜单的“Create New Database”选项绑定ViewModel中的NewDbPageCommand,第一次跳转到NewDb页面完全正常,但通过NewDb页面的Close按钮(绑定DbViewmodel中的ClosePageCommand,调用Shell.Current.GoToAsync(".."))返回主页面后,再次点击该菜单选项跳转时,没有任何反应且无报错信息。已引入CommunityToolkit.Mvvm和CommunityToolkit.Maui库,相关代码如下:
主页面XAML
<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="LockAndKeyMaui.View.PasswdInfo" xmlns:viewmodel="clr-namespace:LockAndKeyMaui.ViewModel" x:DataType="viewmodel:Viewmodel" BackgroundColor="DarkGreen"> <ContentPage.MenuBarItems> <MenuBarItem Text="Passwords"> <MenuFlyoutItem Text="Create New Database" Command="{Binding NewDbPageCommand}"/> <MenuFlyoutItem Text="Select Database"/> </MenuBarItem> <MenuBarItem Text="Groups"/> <MenuBarItem Text="Exit"> <MenuFlyoutItem Text="Close Program" Command="{Binding EndPrgCommand}"/> </MenuBarItem> </ContentPage.MenuBarItems> </ContentPage>
主页面ViewModel代码
using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using LockAndKeyMaui.View; namespace LockAndKeyMaui.ViewModel { public partial class Viewmodel : ObservableObject { [RelayCommand] void EndPrg() { Application.Current.Quit(); } [RelayCommand] void NewDbPage() { Shell.Current.GoToAsync(nameof(NewDb)); } } }
NewDb页面XAML
<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="LockAndKeyMaui.View.NewDb" xmlns:vwmodel="clr-namespace:LockAndKeyMaui.ViewModel" x:DataType="vwmodel:DbViewmodel" BackgroundColor="DarkGreen" Title="NewDb"> <VerticalStackLayout> <Button Text="Close" WidthRequest="150" HeightRequest="50" HorizontalOptions="Center" FontAttributes="Bold" FontSize="Small" Command="{Binding ClosePageCommand}"/> </VerticalStackLayout> </ContentPage>
NewDb页面ViewModel代码
using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; namespace LockAndKeyMaui.ViewModel { public partial class DbViewmodel : ObservableObject { [RelayCommand] void ClosePage() { Shell.Current.GoToAsync(".."); } } }
可能的原因分析
- Shell导航栈异常:使用
GoToAsync("..")返回时,可能未正确将NewDb页面从导航栈中移除,导致二次跳转时Shell判定目标页面状态异常,无法触发跳转。可尝试用绝对路径返回(如GoToAsync("//PasswdInfo")),确保导航栈正确重置。 - RelayCommand触发失效:检查
NewDbPageCommand在页面返回后是否仍能触发。可在NewDbPage方法中添加调试输出(如Console.WriteLine("NewDbPageCommand执行")),确认命令是否被调用。若未触发,可能是主页面ViewModel的绑定上下文在返回后丢失,需确保ViewModel在页面生命周期中保持稳定。 - 页面实例未释放:NewDb页面返回后,其页面或ViewModel实例可能未被正确释放,导致Shell无法创建新实例。可在导航时添加参数强制创建新实例,比如
Shell.Current.GoToAsync($"{nameof(NewDb)}?refresh=true"),并在NewDb页面的OnNavigatedTo方法中处理该参数。 - Shell路由注册问题:确认
AppShell.xaml或代码中是否正确注册了NewDb页面的路由。未正确注册会导致导航逻辑异常,正确注册示例:
或代码注册:<!-- 在AppShell.xaml中注册 --> <ShellContent Route="NewDb" ContentTemplate="{DataTemplate view:NewDb}" />// 在App.xaml.cs或AppShell构造函数中注册 Routing.RegisterRoute(nameof(NewDb), typeof(NewDb));
内容的提问来源于stack exchange,提问作者user22995906
相关产品推荐
相关产品推荐

