Xamarin.Forms:入职页面快速导航回上一页,试图排除导航流程故障并防止立即返回

hof1towb  于 2023-06-27  发布在  其他
关注(0)|答案(1)|浏览(147)

我正在开发一个Xamarin.Forms应用程序,其中包含一个onboarding特性。当满足某些条件时,会显示引导页面,但我面临一个问题,即OnboardingPage在允许用户与之交互之前会快速导航回上一个页面DashboardPage
以下是当前的导航流程:
应用程序检查用户是否已登录。如果已登录,它将导航到 Jmeter 板页面。在 Jmeter 板页面构造器中,它检查是否应显示载入。如果满足这些条件,它将使用Shell.Current.Navigation.PushModalAsync()导航到引导页面。在引导页面中,有一个按钮可以使用Navigation.PopModalAsync()关闭模式页面。问题是,在显示OnboardingPage之后,它会快速导航回 Jmeter 板页面,而不允许用户与引导内容进行交互。第二个导航到DashboardPage似乎有一个动画,并缺乏导航栏,这表明它是推(或弹出?)作为模式页面。
我想确保用户在OnboardingPage上停留,直到它完成。如何修改导航流并正确处理条件,以防止立即返回到 Jmeter 板页面?代码片段如下:

  1. EntryShell.xaml.cs:
public partial class EntryShell : Xamarin.Forms.Shell
{
    public EntryShell()
    {
        InitializeComponent();
        Routing.RegisterRoute("Main1Page", typeof(Main1Page));
        Routing.RegisterRoute("Onboarding", typeof(OnboardingPage));
        Routing.RegisterRoute("LoginPage", typeof(LoginPage));
        Routing.RegisterRoute("RegistrationPage", typeof(RegistrationPage));
        Routing.RegisterRoute("DashboardPage", typeof(DashboardPage));
    }
}

EntryShell类使用Routing.RegisterRoute方法注册各个页面的路由。这段代码看起来是正确的,应该能够根据指定的路径导航到注册的页面。

  1. App.xaml.cs:
public partial class App : Application
{
    Page main1Page, dashboardPage;
    BaseViewModel so = new BaseViewModel();

    public App ()
    {
        InitializeComponent();
        MainPage = new EntryShell();

        if (so.IsLogged)
        {
            Current.ModalPopping += Current_ModalPopping;
            dashboardPage = new DashboardPage();
            Shell.Current.Navigation.PushModalAsync(dashboardPage);
        }
    }

    private void Current_ModalPopping(object sender, ModalPoppingEventArgs e)
    {
        if (e.Modal == main1Page)
        {
            main1Page = null;
            Current.ModalPopping -= Current_ModalPopping;
        }
        else if (e.Modal == dashboardPage)
        {
            dashboardPage = null;
            Current.ModalPopping -= Current_ModalPopping;
        }
    }
}

App类初始化应用程序。如果BaseViewModel示例(so)的IsLogged属性为true,则它将设置dashboardPage示例并使用Shell.Current.Navigation.PushModalAsync()将其作为模态页面推送。这段代码看起来很好,如果用户已经登录,应该可以导航到 Jmeter 板页面。

  1. DashboardPage.xaml.cs:
public partial class DashboardPage : ContentPage
{
    Page onboardingPage = new OnboardingPage();

    public DashboardPage()
    {
        InitializeComponent();
        Debug.WriteLine("Dashboard Constructor Called!");

        if (ShouldShowOnboarding() == true)
        {
            Navi2Onboard();
        }
    }

 private bool ShouldShowOnboarding()
        {

            return true;
            //return VersionTracking.IsFirstLaunchEver;

        }

    private async void Navi2Onboard()
    {
        await Shell.Current.Navigation.PushModalAsync(onboardingPage);
    }
}

DashboardPage类表示 Jmeter 板页面。在构造函数中,它通过调用ShouldShowOnboarding()方法检查是否应该显示载入。如果返回true,则使用Shell.Current.Navigation.PushModalAsync()导航到引导页面。这个代码似乎是正确的。

  1. OnboardingPage.xaml.cs:
public partial class OnboardingPage : ContentPage
{
    public OnboardingPage ()
    {
        InitializeComponent ();
        Debug.WriteLine("Onboard Constructor Called!");
    }

    private async void Button_Clicked(System.Object sender, System.EventArgs e)
    {
        await FadeBox.FadeTo(1, 1000);
        await Navigation.PopModalAsync(false);
    }
}

OnboardingPage类表示入门页面。它包含一个按钮单击事件处理程序,该事件处理程序在一个框中淡入,并通过调用Navigation.PopModalAsync()来关闭模式页面。此代码显示正确。

  1. Main1Page.xaml.cs:
public partial class Main1Page : ContentPage
{
    BaseViewModel

 x = new BaseViewModel();

    public Main1Page()
    {
        InitializeComponent();
        Debug.WriteLine("Point 1");
    }

    private async void SignUpClicked(System.Object sender, System.EventArgs e)
    {
        await Shell.Current.Navigation.PushAsync(new RegistrationPage());
    }

    private async void LoginClicked(System.Object sender, System.EventArgs e)
    {
        await Shell.Current.Navigation.PushAsync(new LoginPage());
    }

    private async void TapGestureRecognizer_Tapped(System.Object sender, System.EventArgs e)
    {
        await Shell.Current.Navigation.PushModalAsync(new DashboardPage());
    }

    private async void GoToWebsite(System.Object sender, System.EventArgs e)
    {
        await Shell.Current.Navigation.PushAsync(new Webview());
    }
}

Main1Page类表示应用程序中的一个页面。它包含各种按钮单击的事件处理程序,这些按钮单击使用Shell.Current.Navigation.PushAsync()Shell.Current.Navigation.PushModalAsync()导航到不同的页面。假设正确定义了必要的页面(RegistrationPageLoginPageDashboardPageWebview),代码看起来很好。

<EntryShell xmlns="http://xamarin.com/schemas/2014/forms"
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            xmlns:local="clr-namespace:YourNamespace"
            x:Class="YourNamespace.EntryShell">
    <TabBar>
        <ShellContent Route="Main1Page" Shell.FlyoutBehavior="Disabled" NavigationPage.HasNavigationBar="False" ContentTemplate="{DataTemplate local:Main1Page}" />
    </TabBar>

    <TabBar >
        <ShellContent Route="DashboardPage" Shell.FlyoutBehavior="Disabled" ContentTemplate="{DataTemplate local:DashboardPage}"/>
    </TabBar>

    <TabBar>
        <ShellContent Route="Onboarding" Shell.FlyoutBehavior="Disabled" ContentTemplate="{DataTemplate local:OnboardingPage}"/>
    </TabBar>

    <TabBar>
        <ShellContent Route="LoginPage" Shell.FlyoutBehavior="Disabled" ContentTemplate="{DataTemplate local:LoginPage}" />
    </TabBar>

    <TabBar>
        <ShellContent Route="RegistrationPage" Shell.FlyoutBehavior="Disabled" ContentTemplate="{DataTemplate local:RegistrationPage}" />
    </TabBar>
</EntryShell>

代码片段表示EntryShell.xaml文件,该文件定义了基于Shell的应用程序的结构。它包括多个TabBar部分,每个部分都包含一个ShellContent元素,指定相关页面的路由和内容模板。这是为了更清楚而提供的。
根据提供的代码片段,导航逻辑似乎是正确的。任何关于解决此导航问题的见解或建议将不胜感激。提前感谢您的帮助!

ibrsph3r

ibrsph3r1#

我把Shell.Current.Navigation.PushAsync()的每个示例都换成了Shell.Current.GoToAsync(),并保持Shell.Current.Navigation.PushModalAsync()的加载不变:
await Shell.Current.Navigation.PushModalAsync(new DashboardPage());
await Shell.Current.GoToAsync("//DashboardPage");
DashboardPage是在EntryShell.xaml文件中注册的路由。这样做后,入门页面将按其应有的样子显示。它必须与Shell.Current.Navigation有关,这使得它表现得很奇怪。

相关问题