dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.24k stars 1.76k forks source link

DisplayAlert is documented as a Modal dialog but cannot be closed or dismissed and isnt included in the Navigation ModalStack #13778

Open duindain opened 1 year ago

duindain commented 1 year ago

Description

Allow the existing Navigation modal stacks to contain DisplayAlerts so they can be checked and closed if necessary

There is an old enhancement request for Xamarin Forms for the same feature https://github.com/xamarin/Xamarin.Forms/issues/6359

Public API Changes

Add DisplayAlerts to the Application.Current.MainPage.Navigation.ModalStack Or expose the stack that contains them so they can be checked and closed or manipulated if needed

Intended Use-Case

The app i am working on has DisplayAlerts for user confirmation for various actions, if the device suspends then later resumes we need to be able to dismiss those alerts as they are no longer relevant

I need to be able to close DisplayAlerts programmatically and to know when one is currently being shown

jfversluis commented 1 year ago

I think there is a mixup here. The navigation stack and a modal are two separate things imho. The navigation stack contains pages that you are navigating to and from. A modal is a short-lived dialog that you are showing on top of those just to notify the user or capture a small piece of information.

The issue you linked on the Forms repo doesn't mention the navigation stack at all. What makes more sense to me is to be able to pass in a CancellationToken that you can cancel on a modal dialog.

ghost commented 1 year ago

We've added this issue to our backlog, and we will work to address it as time and resources allow. If you have any additional information or questions about this issue, please leave a comment. For additional info about issue management, please read our Triage Process.

duindain commented 1 year ago

@jfversluis I was referring to the Modal stack here Application.Current.MainPage.Navigation.ModalStack

which we add modals to display using Application.Current.MainPage.Navigation.PushModalAsync

are DisplayAlerts not modal and shouldn't use the ModalStack? your description of a modal is basically exactly what the DisplayAlerts are

A modal is a short-lived dialog that you are showing on top of those just to notify the user or capture a small piece of information.

And I assume exactly what the ModalStack is for? though presently only the items we add ourselves are showing up there

Passing cancellation tokens seems like a good solution, though I don't understand why they aren't already in the ModalStack if they are infact modals as you and the documentation seem to be saying

I mentioned the Forms issue only to Highlight other use cases from my own and that it was a long running request to be able to have the ability to close the alerts not to say that my use case is identical to theirs or my suggested idea of having the modals added to the modalstack is for some reason unworkable ;)

axa88 commented 1 year ago

This issue to me appears simply that DisplayAlerts should be programmatically closable. As not being able to do so leads to various issues.

This issue is even more irritating in multi window applications when awaiting a Page DisplayAlert override which returns user input. For when the Page's Application.Window is closed by the user during the await, being unable to dismiss the DisplayAlert causes various issues.

I have found at least on the Windows platform, it is possible to override or subscribe to the Window OnDestroying event and extend the DisplayAlert Task with WaitAsync in order to use a CancellationToken override to end/cancel the awaiting of the DisplayAlert prior to the Window being disposed of.

But on Android, Window OnDestroying doesn't even get called. You can use MauiAppBuilder ConfigureLifecycleEvents extension method to configure and capture the platform specific OnDestroy event, and use a cancellation token on the awaiting DisplayAlert, but you will still end up with a Window Leaked so regardless there still appears to be an issue.

I see no any way to avoid this leak; yet it's on the back burner. On a single Window app, closing the window is going to shut down the application anyway, so that avoids an issue I suppose, but on a multi window app, closing the window does not close the app and it leaks. Please educate me if im wrong here, but being unable to dismiss the DisplayAlert appears to be a leak on multi Window applications and should be a first class issue to be solved.