如何在onappearing()方法中导航?

发布于 2025-01-31 23:55:11 字数 1460 浏览 2 评论 0原文

我想避免将我的上登机在导航堆栈中查看,以解决按下返回按钮的问题。在我的mainpage中,我检查inboarding是否应出现。如果为true,我将pushmodalasync方法inppearting方法中的方法。但是,应用程序崩溃具有以下例外:

Objective-C exception thrown.  Name: System.InvalidOperationException Reason: Window should have a UIWindow set. (System.InvalidOperationException)
   at Microsoft.Maui.Controls.Window.get_NativeWindow()
   at Microsoft.Maui.Controls.Platform.ModalNavigationManager.EndEditing()
   at Microsoft.Maui.Controls.Platform.ModalNavigationManager.PushModalAsync(Page modal, Boolean animated)
   at Microsoft.Maui.Controls.Window.NavigationImpl.OnPushModal(Page modal, Boolean animated)
   at BytePesaApp.MainPage.OnAppearing() in MainPage.xaml.cs:line 18
   at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_0(Object state)
   at Foundation.NSAsyncSynchronizationContextDispatcher.Apply()

以下是代码实现:

public partial class MainPage : ContentPage
{

    public MainPage()
    {
        InitializeComponent();
    }

    protected override async void OnAppearing()
    {
        base.OnAppearing();

        if (ShouldShowOnBoardingPage() == true)
        {
            await Navigation.PushModalAsync(new OnBoardingPage(), false);
        }
    }


    private bool ShouldShowOnBoardingPage()
    {
        return VersionTracking.IsFirstLaunchEver;
    }
}

I want to avoid putting my OnBoarding view in navigation stack to fix the problem of pressing the back button. In my MainPage I check if OnBoarding should appear or not. If true, I call PushModalAsync method in OnAppearing method. But the App crash with the following exception:

Objective-C exception thrown.  Name: System.InvalidOperationException Reason: Window should have a UIWindow set. (System.InvalidOperationException)
   at Microsoft.Maui.Controls.Window.get_NativeWindow()
   at Microsoft.Maui.Controls.Platform.ModalNavigationManager.EndEditing()
   at Microsoft.Maui.Controls.Platform.ModalNavigationManager.PushModalAsync(Page modal, Boolean animated)
   at Microsoft.Maui.Controls.Window.NavigationImpl.OnPushModal(Page modal, Boolean animated)
   at BytePesaApp.MainPage.OnAppearing() in MainPage.xaml.cs:line 18
   at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_0(Object state)
   at Foundation.NSAsyncSynchronizationContextDispatcher.Apply()

Here is the code implementation:

public partial class MainPage : ContentPage
{

    public MainPage()
    {
        InitializeComponent();
    }

    protected override async void OnAppearing()
    {
        base.OnAppearing();

        if (ShouldShowOnBoardingPage() == true)
        {
            await Navigation.PushModalAsync(new OnBoardingPage(), false);
        }
    }


    private bool ShouldShowOnBoardingPage()
    {
        return VersionTracking.IsFirstLaunchEver;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

み零 2025-02-07 23:55:11

听起来毛伊岛直到表现出来之前都没有良好的状态。推迟这样的导航:

Dispatcher.Dispatch(async() =>
    await Navigation.PushModalAsync(new OnBoardingPage(), false);
);

此代码将是排队,并将在启动返回后运行。


替代方案是在入职期间替换主页。
在app.xaml.cs中:

if (ShouldShowOnBoardingPage() == true)
    MainPage = new OnBoardingPage();
else
    MainPage = new MainPage();

然后,当入门完成时:

    Application.Current.MainPage = new MainPage();

如果可能的话,我更喜欢后一种方法。

It sounds like Maui is not in a good state to navigate, until OnAppearing finishes. DEFER the navigation like this:

Dispatcher.Dispatch(async() =>
    await Navigation.PushModalAsync(new OnBoardingPage(), false);
);

This code will be queued, and will run after OnAppearing returns.


An ALTERNATIVE is to replace the MainPage during OnBoarding.
In App.xaml.cs:

if (ShouldShowOnBoardingPage() == true)
    MainPage = new OnBoardingPage();
else
    MainPage = new MainPage();

Then when OnBoarding is complete:

    Application.Current.MainPage = new MainPage();

I prefer the latter approach, if possible.

无所谓啦 2025-02-07 23:55:11

onappeation()启动加载的事件;不幸的是,没有onload()可以覆盖,因此您必须使用事件:

public partial class MainPage : ContentPage
{

    public MainPage()
    {
        InitializeComponent();

        this.Loaded += MainPage_Loaded;
    }

    protected async void MainPage_Loaded(object sender, EventArgs e)
    {
        if (ShouldShowOnBoardingPage() == true)
        {
            await Navigation.PushModalAsync(new OnBoardingPage(), false);
        }
    }

    private bool ShouldShowOnBoardingPage()
    {
        return VersionTracking.IsFirstLaunchEver;
    }
}

The Loaded-Event is fired after OnAppearing(); unfortunately, there is no OnLoad() to override, so you have to use the event:

public partial class MainPage : ContentPage
{

    public MainPage()
    {
        InitializeComponent();

        this.Loaded += MainPage_Loaded;
    }

    protected async void MainPage_Loaded(object sender, EventArgs e)
    {
        if (ShouldShowOnBoardingPage() == true)
        {
            await Navigation.PushModalAsync(new OnBoardingPage(), false);
        }
    }

    private bool ShouldShowOnBoardingPage()
    {
        return VersionTracking.IsFirstLaunchEver;
    }
}
柠檬色的秋千 2025-02-07 23:55:11

决定之一是使用任务:

protected override async void OnAppearing()
{
    base.OnAppearing();

    if (ShouldShowOnBoardingPage() == true)
    {
        Task.Run(async () => 
        {
             await Navigation.PushModalAsync(new OnBoardingPage(), false);
        });
        
    }
}

one of the decisions is to use task:

protected override async void OnAppearing()
{
    base.OnAppearing();

    if (ShouldShowOnBoardingPage() == true)
    {
        Task.Run(async () => 
        {
             await Navigation.PushModalAsync(new OnBoardingPage(), false);
        });
        
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文