如何在onappearing()方法中导航?
我想避免将我的上登机
在导航堆栈中查看,以解决按下返回按钮的问题。在我的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
听起来毛伊岛直到表现出来之前都没有良好的状态。推迟这样的导航:
此代码将是排队,并将在启动返回后运行。
替代方案是在入职期间替换主页。
在app.xaml.cs中:
然后,当入门完成时:
如果可能的话,我更喜欢后一种方法。
It sounds like Maui is not in a good state to navigate, until OnAppearing finishes. DEFER the navigation like this:
This code will be queued, and will run after OnAppearing returns.
An ALTERNATIVE is to replace the MainPage during OnBoarding.
In App.xaml.cs:
Then when OnBoarding is complete:
I prefer the latter approach, if possible.
onappeation()
以启动加载的事件;不幸的是,没有
onload()
可以覆盖,因此您必须使用事件:The Loaded-Event is fired after
OnAppearing()
; unfortunately, there is noOnLoad()
to override, so you have to use the event:决定之一是使用任务:
one of the decisions is to use task: