diff --git a/WinUIGallery/App.xaml.cs b/WinUIGallery/App.xaml.cs index 0b1edaa04..8f7a6fd66 100644 --- a/WinUIGallery/App.xaml.cs +++ b/WinUIGallery/App.xaml.cs @@ -8,6 +8,7 @@ using Microsoft.Windows.AppNotifications.Builder; using Microsoft.Windows.BadgeNotifications; using System; +using System.Collections.Generic; using System.Diagnostics; using System.Linq; using Windows.ApplicationModel.Activation; @@ -60,6 +61,23 @@ protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs ar MainWindow.Closed += (s, e) => { BadgeNotificationManager.Current.ClearBadge(); + + // Close all remaining active windows to prevent resource disposal conflicts + var activeWindows = new List(WindowHelper.ActiveWindows); + foreach (var window in activeWindows) + { + if (window != s) // Don't try to close the window that's already closing + { + try + { + window.Close(); + } + catch + { + // Ignore any exceptions during cleanup + } + } + } }; } diff --git a/WinUIGallery/Samples/ControlPages/CreateMultipleWindowsPage.xaml.cs b/WinUIGallery/Samples/ControlPages/CreateMultipleWindowsPage.xaml.cs index 289dadaee..3ad384e77 100644 --- a/WinUIGallery/Samples/ControlPages/CreateMultipleWindowsPage.xaml.cs +++ b/WinUIGallery/Samples/ControlPages/CreateMultipleWindowsPage.xaml.cs @@ -3,6 +3,7 @@ using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; +using WinUIGallery.Helpers; using WinUIGallery.Pages; namespace WinUIGallery.ControlPages; @@ -17,6 +18,7 @@ public CreateMultipleWindowsPage() private void createNewWindow_Click(object sender, RoutedEventArgs e) { var newWindow = new MainWindow(); + WindowHelper.TrackWindow(newWindow); newWindow.Activate(); var targetPageType = typeof(HomePage);