Maui导航到单例页面时报错解决方案

Maui导航到单例页面时报错解决方案

当使用Shell.Current.GoToAsync导航到单例页面时,如果未取消动画显示将有一定的概率提示Microsoft.UI.Xaml.Controls.Frame.NavigationFailed was unhandled错误。
所以为了避免此类问题发生将调用参数animate设置为false,即await Shell.Current.GoToAsync($"..", false)可以减少错误发生的机率,同时要加入try catch进行捕捉处理。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
        public static async Task GoToAsync(string uri)
        {
            string sourceUri = Shell.Current.CurrentState.Location.OriginalString;
            try
            {
                await Shell.Current.GoToAsync(uri, false);
            }
            catch
            {
                Shell.Current.GoToAsync(sourceUri, false);
                _ = Task.Run(() =>
                MainThread.BeginInvokeOnMainThread(() =>
                {
                    Task.Delay(50).Wait();
                    Shell.Current.GoToAsync(uri, false);
                }));
            }
        }
Licensed under CC BY-NC-SA 4.0
Built with Hugo
Theme Stack designed by Jimmy