diff --git a/src/CommunityToolkit.Maui.MediaElement/Handlers/MediaElementHandler.windows.cs b/src/CommunityToolkit.Maui.MediaElement/Handlers/MediaElementHandler.windows.cs index 17d253f49..63a25e5f1 100644 --- a/src/CommunityToolkit.Maui.MediaElement/Handlers/MediaElementHandler.windows.cs +++ b/src/CommunityToolkit.Maui.MediaElement/Handlers/MediaElementHandler.windows.cs @@ -32,7 +32,27 @@ protected override MauiMediaElement CreatePlatformView() protected override void DisconnectHandler(MauiMediaElement platformView) { Dispose(); - platformView.Dispose(); + UnloadPlatformView(platformView); base.DisconnectHandler(platformView); } + + static void UnloadPlatformView(MauiMediaElement platformView) + { + if (platformView.IsLoaded) + { + platformView.Unloaded += OnPlatformViewUnloaded; + } + else + { + platformView.Dispose(); + } + + static void OnPlatformViewUnloaded(object sender, Microsoft.UI.Xaml.RoutedEventArgs e) + { + var mediaElement = (MauiMediaElement)sender; + + mediaElement.Unloaded -= OnPlatformViewUnloaded; + mediaElement.Dispose(); + } + } } \ No newline at end of file