From 9e97cd84750f46e24df011cc2cac81cdea21bb12 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 11 Dec 2024 11:05:58 -0800 Subject: [PATCH 1/2] adding count check for navigation stack before getting last-1, and adding check for mainpage as last resort --- .../Views/MauiMediaElement.macios.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/CommunityToolkit.Maui.MediaElement/Views/MauiMediaElement.macios.cs b/src/CommunityToolkit.Maui.MediaElement/Views/MauiMediaElement.macios.cs index 82fa4cc342..ad73e5cdea 100644 --- a/src/CommunityToolkit.Maui.MediaElement/Views/MauiMediaElement.macios.cs +++ b/src/CommunityToolkit.Maui.MediaElement/Views/MauiMediaElement.macios.cs @@ -157,12 +157,19 @@ static bool TryGetCurrentPage([NotNullWhen(true)] out Page? currentPage) } // If not using Shell or a Modal Page, return the visible page in the (non-modal) NavigationStack - if (window.Navigation.NavigationStack[^1] is Page page) + if (window.Navigation.NavigationStack.Count > 0 && window.Navigation.NavigationStack[^1] is Page page) { currentPage = page; return true; } + if (Application.Current != null && Application.Current.MainPage != null && Application.Current.MainPage is Page mainPage) + { + currentPage = mainPage; + return true; + } + + return false; static bool TryGetModalPage(Window window, [NotNullWhen(true)] out Page? page) From 7233d7d40d421788a4e0da77b466faece12c7c85 Mon Sep 17 00:00:00 2001 From: Mike Simon Date: Fri, 3 Jan 2025 02:16:21 -0800 Subject: [PATCH 2/2] Removing obsolete code for Application.Current.MainPage --- .../Views/MauiMediaElement.macios.cs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/CommunityToolkit.Maui.MediaElement/Views/MauiMediaElement.macios.cs b/src/CommunityToolkit.Maui.MediaElement/Views/MauiMediaElement.macios.cs index ad73e5cdea..d2c917f0d0 100644 --- a/src/CommunityToolkit.Maui.MediaElement/Views/MauiMediaElement.macios.cs +++ b/src/CommunityToolkit.Maui.MediaElement/Views/MauiMediaElement.macios.cs @@ -163,12 +163,6 @@ static bool TryGetCurrentPage([NotNullWhen(true)] out Page? currentPage) return true; } - if (Application.Current != null && Application.Current.MainPage != null && Application.Current.MainPage is Page mainPage) - { - currentPage = mainPage; - return true; - } - return false;