毛伊岛贝壳导航失败 - 罕见的复制品

pin*_*2k4 5 c# maui

我在使用 MAUI 应用程序时遇到问题,很少出现未处理的 NavigationFailed 异常。我有一个主视图,它导航到具有以下内容的辅助页面:

await Shell.Current.GoToAsync($"{nameof(AddCollectionPage)}", true, new Dictionary<string, object>
{
    {"Contracts", queryStringBuilder.ToString() }
});
Run Code Online (Sandbox Code Playgroud)

然后第二页中有一个按钮可以向后返回,使用以下命令:

await Shell.Current.GoToAsync("../");
Run Code Online (Sandbox Code Playgroud)

现在效果很好,1000 次中有 999 次可能没有问题……但应用程序崩溃的情况很少见。在调试时,它在 App.gics 中的 UnhandledException 点处中断,代码如下:

UnhandledException += (sender, e) =>
{
    if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break();
};
Run Code Online (Sandbox Code Playgroud)

例外并不是那么有用。 在此输入图像描述

调用堆栈仅包含 4 个条目 - 其中 2 个是外部代码,1 个是我在 App.gics 中发布的位置,另一个是调用 shell 导航的行。

刚才就遇到了这个问题,在代码中我调用导航从辅助页面返回主页面的“../”。这种情况确实很少见,但频率足够高,非常烦人。

当我包装调用 GoToAsync 的行(这会导致 try catch 中崩溃并记录它捕获的异常)时,我得到以下信息(错误和堆栈跟踪)。其中的底线引用了我的代码中调用 GoToAsync 的部分,其他所有内容都是 MAUI 框架的内容。

System.Runtime.InteropServices.COMException (0x80004005): Error HRESULT E_FAIL has been returned from a call to a COM component.
   at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|20_0(Int32 hr)
   at ABI.Microsoft.UI.Xaml.Controls.IFrameMethods.GoBack(IObjectReference _obj, NavigationTransitionInfo transitionInfoOverride)
   at Microsoft.Maui.Handlers.ElementHandler.Invoke(String command, Object args)
   at Microsoft.Maui.Controls.ShellSection.OnPopAsync(Boolean animated)
   at Microsoft.Maui.Controls.ShellSection.PrepareCurrentStackForBeingReplaced(ShellNavigationRequest request, ShellRouteParameters queryData, IServiceProvider services, Nullable`1 animate, List`1 globalRoutes, Boolean isRelativePopping)
   at Microsoft.Maui.Controls.ShellSection.GoToAsync(ShellNavigationRequest request, ShellRouteParameters queryData, IServiceProvider services, Nullable`1 animate, Boolean isRelativePopping)
   at Microsoft.Maui.Controls.ShellNavigationManager.GoToAsync(ShellNavigationParameters shellNavigationParameters, ShellNavigationRequest navigationRequest)
   at BlurFarm.ViewModels.AddCollectionPageViewModel.SelectCollection(Collection collection)
Run Code Online (Sandbox Code Playgroud)

Too*_*eve 0

当然看起来像毛伊岛的虫子。

  • 我建议创建一个公共 github 存储库,其中包含一个简单的项目,其中包含足够的代码来运行和显示问题。从 Maui 模板创建一个新项目,并保持两个页面简单。还会发生吗?

  • 看看这个 hack 是否可以避免这个问题:

MainThread.BeginInvokeOnMainThread(async () =>
{
   await Task.Delay(200);
   await Shell.Current.GoToAsync("../");
}
Run Code Online (Sandbox Code Playgroud)

这有两件事:

  1. 让按钮方法在执行任何操作之前返回。
  2. 引入轻微的延迟。

我的想法是,毛伊岛有时处于“未准备好”执行 GoToAsync 的状态。我无法想象为什么会出现这种情况,但值得一试。