2

I want to show an ALERT/POPUP to the user to ask him/her before he closes the Application.

This is what I am already doing :

In App.xaml.cs

Application.Current.MainPage.DisplayAlert this line throws unhandled exception,

The Exception gets unhandled by debugger itself. and it says :

a debugger is attached to but not configured to debug this unhandled exception.

https://github.com/BhangeeF16/MAUI-DOT-NET/blob/main/SampleApp/App.xaml.cs

public partial class App : Application
{
    public static IServiceProvider Services;

    public App(IServiceProvider serviceProvider)
    {
        InitializeComponent();
        Services = serviceProvider;
        MainPage = new MainPage();
    }
    protected override Window CreateWindow(IActivationState activationState)
    {
        Window window = base.CreateWindow(activationState);
        window.Page = MainPage;
        window.Destroying += Window_Destroying;
        return window;
    }

    private void Window_Destroying(object sender, EventArgs e)
    {
        try
        {
            System.Diagnostics.Debug.WriteLine("Destroying");
            App.Current.Dispatcher.Dispatch(async () =>
            await App.Current.MainPage.DisplayAlert("Alert", "Are you sure you want to close the application.", "Yes", "No"));    
        }
        catch (Exception)
        {
        }
    }
}
BNG016
  • 164
  • 1
  • 7

1 Answers1

1

I would suggest you read and understand the life cycle of an MAUI app: Docs

The event you are looking for should either be OnStopped, or OnDestroying, dependant on your needs.

monstertjie_za
  • 7,277
  • 8
  • 42
  • 73
  • 1
    I read the documentation, implemented an override for it, the issue now is that I cant get the alert to show. Every time I try to show an Alert it throws an exception @monstertjie_za – BNG016 Aug 11 '22 at 11:07