XAML MAUI:await Shell.Current.GoToAsync with Modal presentation doesn't work as expected

abithluo  于 2023-10-14  发布在  Shell
关注(0)|答案(1)|浏览(138)

我是一个MAUI和移动的应用程序开发的新手,在最近的iOS更新后,我面临着从Xamarin.Forms到MAUI的强制迁移。问题是MAUI的模式导航并不像我期望的那样工作。代码如下:

private async void btnSend_Tapped(object sender, EventArgs e)
{
    await Shell.Current.GoToAsync("//MainPage/TakePicture/SendPicture/OtpCheck");

    if (Variables.CurrentPicture.Otp == 0)
    {
        ....
    }

    await Shell.Current.Navigation.PopToRootAsync();

在我设置的OtpCheck页面的xaml中:

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             ...
             x:Class="SkyObservers.Pages.OtpCheck"
             Shell.PresentationMode="Modal">

我希望“if(Variables...”只有在OtpCheck页面关闭时才能到达,但它并不像这样工作。页面被加载,然后突然消失,因为“PopToRootAsync”立即到达。在Xamarin中。表单不能这样工作,所以我也尝试了
await Navigation.PushModalAsync(new OtpCheck());
而不是
await Shell.Current.GoToAsync("//MainPage/TakePicture/SendPicture/OtpCheck");
但结果是一样的
P.S.页面的路由(希望)在AppShell.xaml中正确注册:

<ShellContent Title="SkyObservers" ContentTemplate="{DataTemplate local:MainPage}" Route="MainPage" />
<ShellContent Title="Take New Picture" ContentTemplate="{DataTemplate pages:TakePicture}" Route="MainPage/TakePicture" />
<ShellContent Title="Send Picture" ContentTemplate="{DataTemplate pages:SendPicture}" Route="MainPage/TakePicture/SendPicture" />
<ShellContent Title="Check OTP" ContentTemplate="{DataTemplate pages:OtpCheck}" Route="MainPage/TakePicture/SendPicture/OtpCheck" />

在AppShell.xaml.cs中

public AppShell()
{
    InitializeComponent();

    Routing.RegisterRoute("MainPage/TakePicture", typeof(TakePicture));
    Routing.RegisterRoute("MainPage/TakePicture/SendPicture", typeof(SendPicture));
    Routing.RegisterRoute("MainPage/TakePicture/SendPicture/OtpCheck", typeof(OtpCheck));
}
8ftvxx2r

8ftvxx2r1#

我已经通过在CommunityToolkit.Maui.Views.Popup中转换OtpCheck页面并等待结果来解决这个问题。页面的Xaml代码:

<?xml version="1.0" encoding="utf-8" ?>
<views:Popup xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:resources="clr-namespace:SkyObservers.Localization"
         xmlns:views="clr-namespace:CommunityToolkit.Maui.Views assembly=CommunityToolkit.Maui"
         x:Class="SkyObservers.Pages.OtpCheck"
         CanBeDismissedByTappingOutsideOfPopup="False"
         Shell.PresentationMode="Modal">

CodeBehind用于打开和等待弹出窗口的结果:

bool result = (bool) await this.ShowPopupAsync(new OtpCheck());

相关问题