Maui - close up with confirmations

Dani_S 4,191 Reputation points
2024-02-26T10:05:45.4433333+00:00

Hi, I'm using .Net 8. I want when user will close the app he will get promp for Windows platform: If confirm it willl do this code otherwise close operation will be cancell ? How is can be done ?

My current code:

            builder
                .UseTelerik()
                .UseMauiApp<App>()
                .ConfigureLifecycleEvents(events =>
                {
#if WINDOWS
events.AddWindows(windowsLifecycleBuilder =>
{
    windowsLifecycleBuilder.OnWindowCreated(window =>
   {        
        var handle = WinRT.Interop.WindowNative.GetWindowHandle(window);
        var id = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(handle);
       var appWindow = Microsoft.UI.Windowing.AppWindow.GetFromWindowId(id);      
        appWindow.Closing += async (s, e) =>
        {
            e.Cancel = true;
           
            try
            {
                if (!string.IsNullOrEmpty(LoginConfigurations.GetInstance().SessionToken) )
                {
                    var container = new Bootstrapper().Bootstrap();
                    IAutomationApiClient automationApiClient = container.Resolve<IAutomationApiClient>();
                   var res = await automationApiClient.PostLogOut($"{LoginConfigurations.GetInstance().Portal}/api/GSPortal/PostLogOut/false", LoginConfigurations.GetInstance().SessionToken);
                }
            }
            catch (Exception)
            {
            }
           App.Current.Quit();
           
                    `
           
        };
    });
});
#endif
                })

Then I use bool answer = await DisplayAlert("Question?", "Would you like to play a game", "Yes", "No"); to popup a window, but DisplayAlert can be not complied in mauiprogram.cs

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
4,006 questions
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 80,771 Reputation points Microsoft External Staff
    2024-02-27T01:10:22.27+00:00

    Hello,

    =================Update=================

    Please do not remove   e.Cancel = true; in the Closing event like following code.

    
    
    
    appWindow.Closing += async (s, e) =>  
    {  
         e.Cancel = true;
          bool answer = await Application.Current.MainPage.DisplayAlert("Question?", "Would you like to play a game", "Yes", "No");
          if (answer)
          {
    
    
          }
          else{
                App.Current.Quit();
          }
    }
    

    bool answer = await DisplayAlert("Question?", "Would you like to play a game", "Yes", "No"); not complied in maui program/create maui app

    You can use Application.Current.MainPage.DisplayAlert to invoke the DisplayAlert in maui program/create maui app like following code.

    bool answer = await Application.Current.MainPage.DisplayAlert("Question?", "Would you like to play a game", "Yes", "No");
    if (answer)
       {
    
    
    }
    else{
         App.Current.Quit();
    }
                                    
    

    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.