Register close button in mac

Dani_S 3,106 Reputation points
2024-06-02T07:04:04.6633333+00:00

Hi,

In window i did, how to do it in mac ?

        builder

            .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;

            if(string.IsNullOrEmpty(LoginConfigurations.GetInstance().SessionToken))

            {

                App.Current.Quit();

                return;

            }

            bool answer = await Application.Current.MainPage.DisplayAlert("Close the application?", "Are you sure you want to close the application?", "Yes", "No",FlowDirection.LeftToRight);

           if(answer) 

           {

       

           

               

       

                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

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

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 70,966 Reputation points Microsoft Vendor
    2024-06-04T01:21:11.6666667+00:00

    Hello,

    Firstly, please enable multi-window support on iPadOS and Mac Catalyst by iPadOS and macOS configuration.

    Then open App.xaml.cs create a window and rewrite the Backgrounding event. After getting value of res, your application will be closed in the background.

    // in the App class
    protected override Window CreateWindow(IActivationState? activationState)
        {
    		Window window = base.CreateWindow(activationState);
            window.Backgrounding += Window_Backgrounding;
          
    		return window;
        }
     
        private async void Window_Backgrounding(object? sender, BackgroundingEventArgs e)
        {
           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).ContinueWith(t => {
    
    // if you get the result. then your application will be quit.
               App.Current.Quit();
    
           }); 
    
    
                        }
    
                     }
    
                    catch (Exception)
    
                    {
    
                    }
        }
    

    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