我正在使用 shell 导航,并且尝试通过菜单从主页导航到第二页。第一次没有问题,但是当我返回第一个屏幕广告并尝试再次进入第二个屏幕时,什么也没有发生。
这是第一页的 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));
}
}
}
现在它正在调用
NewPageDb
方法,并且第一次运行完美。
在我的第二个屏幕中,我有一些如下所示的 XAML:
<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>
ClosePageCommand
看起来像这样:
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;`enter code here`
namespace LockAndKeyMaui.ViewModel
{
public partial class DbViewmodel : ObservableObject
{
[RelayCommand]
void ClosePage()
{
Shell.Current.GoToAsync("..");
}
}
}
我下载了
CommunityToolKit.Mvvm
和CommunityToolKit.Maui
。
正如我所说,当我第一次运行程序时,页面切换第一次就完美地工作了。然而,当我第二次及之后尝试单击该选择进入该页面时,似乎什么也没发生。我什至没有收到错误消息。
那么有什么东西被消灭了吗?可能出了什么问题?
您需要
await
GoToAsync
方法:
[RelayCommand]
private async Task NewDbPage()
{
await Shell.Current.GoToAsync(nameof(NewDb));
}
[RelayCommand]
private async Task ClosePage()
{
await Shell.Current.GoToAsync("..");
}