How to ask user permission before closing MAUI APP?

Lohith K K 110 Reputation points
2023-07-05T18:30:06.65+00:00

I have read about the lifecycle events in maui and found the below:

#if WINDOWS              
events.AddWindows(windows => windows                     
.OnActivated((window, args) => LogEvent(nameof(WindowsLifecycle.OnActivated)))                     .OnClosed((window, args) => LogEvent(nameof(WindowsLifecycle.OnClosed)))                     .OnLaunched((window, args) => LogEvent(nameof(WindowsLifecycle.OnLaunched)))                     .OnLaunching((window, args) => LogEvent(nameof(WindowsLifecycle.OnLaunching)))                     .OnVisibilityChanged((window, args) => LogEvent(nameof(WindowsLifecycle.OnVisibilityChanged)))                     .OnPlatformMessage((window, args) =>
{
	 if (args.MessageId == Convert.ToUInt32("031A", 16))
     {
	   / System theme has changed
     }                     
})); 
#endif
static bool LogEvent(string eventName, string type = null)
{
	System.Diagnostics.Debug.WriteLine($"Lifecycle event: {eventName}{(type == null ? string.Empty : $" ({type})")}");                  
return true;              
}          
});

What I want is, when the user clicks on the close or X button of the window I want to show a display alert asking do you want to close or not?
The button as below:

User's image

Developer technologies | .NET | .NET MAUI
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2023-07-06T02:07:42.7933333+00:00

    Hello,

    What I want is, when the user clicks on the close or X button of the window I want to show a display alert asking do you want to close or not?

    You can do this by adding a window close event to appwindow in the windowsLifecycleBuilder. When the user executes the closing method, we can push a display alert. If the user clicks Yes, close this application; if they click the Cancel, the display alert will dismiss. Here is a simple code that you can refer to.

    #if WINDOWS
    events.AddWindows(windowsLifecycleBuilder =>
    {
        windowsLifecycleBuilder.OnWindowCreated(window =>
        {
            //use Microsoft.UI.Windowing functions for window
            var handle = WinRT.Interop.WindowNative.GetWindowHandle(window);
            var id = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(handle);
            var appWindow = Microsoft.UI.Windowing.AppWindow.GetFromWindowId(id);
    
            //When user execute the closing method, we can push a display alert. If user click Yes, close this application, if click the cancel, display alert will dismiss.
            appWindow.Closing += async (s, e) => 
            {
                e.Cancel = true;
                bool result = await App.Current.MainPage.DisplayAlert(
                    "Alert title", 
                    "You sure want to close app?", 
                    "Yes", 
                    "Cancel");
    
                if (result)
                {
                    App.Current.Quit();
                }
            };
        });
    });
    #endif
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.