Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Windows Media Element in MultiWindow Mode on exit Crash #2259

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions src/CommunityToolkit.Maui/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,27 @@ public void SetShouldEnableSnackbarOnWindows(bool value)
events.AddWindows(windows => windows
.OnLaunched((_, _) =>
{
Microsoft.Windows.AppNotifications.AppNotificationManager.Default.NotificationInvoked += OnSnackbarNotificationInvoked;
Microsoft.Windows.AppNotifications.AppNotificationManager.Default.Register();
if (Application.Current is null)
{
throw new InvalidOperationException($"{nameof(Application)}.{nameof(Application.Current)} cannot be null when Windows are launched");
}

else if (Application.Current.Windows.Count is 1)
{
Microsoft.Windows.AppNotifications.AppNotificationManager.Default.NotificationInvoked += OnSnackbarNotificationInvoked;
Microsoft.Windows.AppNotifications.AppNotificationManager.Default.Register();
}
})
.OnClosed((_, _) =>
{
try
if (Application.Current is null)
{
Microsoft.Windows.AppNotifications.AppNotificationManager.Default.NotificationInvoked -= OnSnackbarNotificationInvoked;
Microsoft.Windows.AppNotifications.AppNotificationManager.Default.Unregister();
throw new InvalidOperationException($"{nameof(Application)}.{nameof(Application.Current)} cannot be null when Windows are closed");
}
catch
else if (Application.Current.Windows.Count is 1)
{
// And Element not found exception may be thrown when unregistering the event handler after using MediaElement accross multiple Windows
Microsoft.Windows.AppNotifications.AppNotificationManager.Default.NotificationInvoked -= OnSnackbarNotificationInvoked;
Microsoft.Windows.AppNotifications.AppNotificationManager.Default.Unregister();
}
}));
});
Expand Down