diff --git a/src/Maui/Prism.Maui/Dialogs/DialogService.cs b/src/Maui/Prism.Maui/Dialogs/DialogService.cs index d36a6d0f4..6d97bd48b 100644 --- a/src/Maui/Prism.Maui/Dialogs/DialogService.cs +++ b/src/Maui/Prism.Maui/Dialogs/DialogService.cs @@ -1,4 +1,5 @@ using Prism.Common; +using Prism.Navigation; #nullable enable namespace Prism.Dialogs; @@ -9,18 +10,36 @@ namespace Prism.Dialogs; public sealed class DialogService : DialogServiceBase { private readonly IPageAccessor _pageAccessor; + private readonly IWindowManager _windowManager; /// /// Creates a new instance of the for Maui Applications /// /// The used to determine where in the Navigation Stack we need to process the Dialog. + /// The used to resolve the current page when the scoped page is no longer attached. /// Throws when any constructor arguments are null. - public DialogService(IPageAccessor pageAccessor) + public DialogService(IPageAccessor pageAccessor, IWindowManager windowManager) { ArgumentNullException.ThrowIfNull(pageAccessor); + ArgumentNullException.ThrowIfNull(windowManager); _pageAccessor = pageAccessor; + _windowManager = windowManager; } /// - protected override Page? GetCurrentPage() => _pageAccessor.Page; + protected override Page? GetCurrentPage() + { + var page = _pageAccessor.Page; + + // If the scoped page is still connected to a window, use it + if (page?.GetParentWindow() is not null) + return page; + + // Fallback: the scoped page was detached (e.g. after absolute navigation). + // Resolve the current page from the active window instead. + if (_windowManager.Current is PrismWindow prismWindow) + return prismWindow.CurrentPage; + + return page; + } }