I am creating a basic MAUI app (Android only) using very basic Shell Navigation. Could anyone confirm (or not) whether the following is a valid way to do this? It works, but when navigating, occasionally the wrong page is briefly displayed before settling on the correct one.
I have no need of Flyout or Tab menus.
All pages are added as Singletons in MauiProgram.cs
. e.g.
builder.Services.AddSingleton<MainPage>();
All pages have routes registered in AppShell.xaml.cs
. e.g.
Routing.RegisterRoute(nameof(MainPage), typeof(MainPage));
All pages have buttons to open other pages using [RelayCommand] in a ViewModel. e.g.
await Shell.Current.GoToAsync(nameof(Page1), false);
The routing structure is flat in AppShell.xaml
:
<?xml version="1.0" encoding="UTF-8" ?>
<Shell
x:Class="MyMauiApp.AppShell"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MyMauiApp.Views"
Shell.FlyoutBehavior="Disabled">
<ShellContent
Title="MainPage"
ContentTemplate="{DataTemplate local:Main开发者_如何学CPage}"
Route="MainPage" />
<ShellContent
Title="Page1"
ContentTemplate="{DataTemplate local:Page1}"
Route="Page1" />
<ShellContent
Title="Page2"
ContentTemplate="{DataTemplate local:Page2}"
Route="Page2" />
etc ...
MainPage goes to Page1,
Page1 to Page2 (or back to MainPage), Page2 to Page3 (or back to Page1), Page3 to Page4 (or back to Page1) etc.It may not go through all pages forwards and backwards in sequence (I would like to be able to go to any page from any other). This works, but particularly when going back (using protected override bool OnBackButtonPressed() in the code behind), a different page is briefly displayed. e.g. in Page3Page.xaml.cs
protected override bool OnBackButtonPressed()
{
page3PageViewModel.PageGoBack();
return true;
}
and in page3PageViewModel.cs
public async void PageGoBack()
{
await Shell.Current.GoToAsync("//" + nameof(Page1), false);
}
The result is right but it looks messy. Suggestions welcome.
精彩评论