.NET 8应用前台点击推送通知崩溃:Window was already created
MAUI应用前台点击推送通知崩溃:Window was already created异常解决
问题描述
我在MAUI应用中配置了推送通知,当应用处于打开状态时点击通知,应用崩溃并抛出如下异常:
[mono-rt] [ERROR] FATAL UNHANDLED EXCEPTION: System.InvalidOperationException: Window was already created [mono-rt] at Microsoft.Maui.Controls.Window.Microsoft.Maui.IWindow.Created() in D:\a\_work\1\s\src\Controls\src\Core\Window\Window.cs:line 491 [mono-rt] at Microsoft.Maui.LifecycleEvents.AppHostBuilderExtensions.<>c.<OnConfigureLifeCycle>b__2_0(Activity activity, Bundle bundle) in D:\a\_work\1\s\src\Core\src\Hosting\LifecycleEvents\AppHostBuilderExtensions.Android.cs:line 23 [mono-rt] at Microsoft.Maui.MauiAppCompatActivity.<>c__DisplayClass8_0.<OnPostCreate>b__0(OnPostCreate del) in D:\a\_work\1\s\src\Core\src\Platform\Android\MauiAppCompatActivity.Lifecycle.cs:line 61 [mono-rt] at Microsoft.Maui.LifecycleEvents.LifecycleEventServiceExtensions.InvokeLifecycleEvents[OnPostCreate](IServiceProvider services, Action`1 action) in D:\a\_work\1\s\src\Core\src\LifecycleEvents\LifecycleEventServiceExtensions.cs:line 31 [mono-rt] at Microsoft.Maui.MauiAppCompatActivity.OnPostCreate(Bundle savedInstanceState) in D:\a\_work\1\s\src\Core\src\Platform\Android\MauiAppCompatActivity.Lifecycle.cs:line 61 [mono-rt] at DailyBudgetMAUIApp.MainActivity.OnPostCreate(Bundle savedInstanceState) in C:\Users\darre\OneDrive\Documents\OneDrive\Documents\DailyBudgetMAUIApp\DailyBudgetMAUIApp\Platforms\Android\MainActivity.cs:line 20 [mono-rt] at Android.App.Activity.n_OnPostCreate_Landroid_os_Bundle_(IntPtr jnienv, IntPtr native__this, IntPtr native_savedInstanceState) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/net8.0/android-34/mcw/Android.App.Activity.cs:line 4488 [mono-rt] at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPL_V(_JniMarshal_PPL_V callback, IntPtr jnienv, IntPtr klazz, IntPtr p0) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:line 125
崩溃发生在OnPostCreate方法中,原因是窗口已存在,但无法推进解决,尝试搜索和调试均无果。
解决方案
1. 配置Activity的LaunchMode
修改Platforms/Android/MainActivity.cs的Activity特性,将LaunchMode设置为SingleTop,避免前台时重复创建Activity实例:
[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, LaunchMode = LaunchMode.SingleTop, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)] public class MainActivity : MauiAppCompatActivity { // 现有代码 }
SingleTop模式下,若Activity已在栈顶,系统会调用OnNewIntent而非创建新实例,从根源避免重复创建窗口。
2. 重写OnNewIntent处理通知数据
在MainActivity中重写OnNewIntent方法,直接处理推送通知的跳转逻辑,绕过可能触发窗口重建的流程:
protected override void OnNewIntent(Intent intent) { base.OnNewIntent(intent); if (intent.Extras != null) { // 提取通知数据 var payload = intent.Extras.GetString("notification_payload"); // 通过Messenger通知MAUI主线程处理导航 WeakReferenceMessenger.Default.Send(new NotificationNavMessage(payload)); } }
同时,确保推送通知的PendingIntent使用FlagUpdateCurrent标记,避免生成重复的Intent:
var pendingIntent = PendingIntent.GetActivity(context, 0, intent, PendingIntentFlags.UpdateCurrent | PendingIntentFlags.Immutable);
3. 清理OnPostCreate中的自定义代码
检查MainActivity.cs的OnPostCreate方法,移除手动触发窗口创建的代码。MAUI框架已自动处理窗口生命周期,无需手动调用:
protected override void OnPostCreate(Bundle savedInstanceState) { base.OnPostCreate(savedInstanceState); // 移除类似 ((IWindow)Window).Created() 的代码 }
4. 更新MAUI版本
该异常可能是MAUI框架的已知生命周期bug,尝试升级到最新稳定版.NET MAUI,官方通常会修复此类窗口管理问题。
内容的提问来源于stack exchange,提问作者Darren Fillis
相关产品推荐
相关产品推荐

