当我在加载另一个页面时操作导航堆栈时,应用程序崩溃。例如,假设导航堆栈中有 Page1 和 Page2,我从 Page2 导航到 Page3。在初始化 Page3 对象时,我从导航堆栈中删除 Page2。 Page3 完成加载后,应用程序崩溃。
MainThread.InvokeOnMainThreadAsync(() =>
{
while (pageElementType != endPageType)
{
if (pageElementType == startPageType)
{
removePageIndex = index + 1;
}
else if (removePageIndex > 0)
{
try
{
// If we comment below line of code of removal page then app doesnt get crashes after navigation to the next page
**navService.RemovePage(navService.NavigationStack[removePageIndex]);**
index--;
}
catch (Exception ex)
{
// Log exception
Common.Log.Error($"{nameof(ManipulateNavStack)}", ex);
}
}
index++;
if (index < navService.NavigationStack.Count)
{
pageElementType = ((BaseViewModel)navService.NavigationStack[index].BindingContext).PageType;
}
else
{
break;
}
}
});
仅在 iOS 应用程序中出现以下异常:
Object reference not set to an instance of an object at Microsoft.Maui.Controls.Handlers.Compatibility.NavigationRenderer.ParentingViewController.WillMoveToParentViewController.
Full Path: System.NullReferenceException: Object reference not set to an instance of an object at Microsoft.Maui.Controls.Handlers.Compatibility.NavigationRenderer.ParentingViewController.WillMoveToParentViewController(UIViewController parent) at UIKit.UIApplication.UIApplicationMain(Int32 argc, String[] argv, IntPtr principalClassName, IntPtr delegateClassName) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:line 61 at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:line 96 at xxxxx.Mobile.Program.Main(String[] args
我尝试从主线程上的导航堆栈中删除页面。
我进行了代码审查并进行了以下更改:
至于您的具体导航,我可以使用向后导航,在路线中使用“..”,即
参考资料: