Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to handle AppWindow closing event before close window on MacOS? #20134

Closed
AllenMiaoFil opened this issue Jan 24, 2024 · 0 comments
Closed
Labels
area-core-lifecycle XPlat and Native UIApplicationDelegate/Activity/Window lifecycle events platform/macOS 🍏 macOS / Mac Catalyst t/bug Something isn't working

Comments

@AllenMiaoFil
Copy link

Description

Hey Team,

I got a question about how I could handle closing event when we clicked close application button on MacOS,

I know there is a closing event when we click 'Close Window' on Windows like below code

#if WINDOWS

        builder.ConfigureLifecycleEvents(events =>
        {
            // Make sure to add "using Microsoft.Maui.LifecycleEvents;" in the top of the file
            events.AddWindows(windowsLifecycleBuilder =>
            {
                windowsLifecycleBuilder.OnWindowCreated(window =>
                {
                    window.ExtendsContentIntoTitleBar = false;
                    var handle = WinRT.Interop.WindowNative.GetWindowHandle(window);
                    var id = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(handle);
                    var appWindow = Microsoft.UI.Windowing.AppWindow.GetFromWindowId(id);
                  
                    switch (appWindow.Presenter)
                    {
                        case Microsoft.UI.Windowing.OverlappedPresenter overlappedPresenter:
                           //disable the max button
                            overlappedPresenter.IsMaximizable = false;
                           break;
                    }

                   //When user execute the closing method, we can make the window do not close by   e.Cancel = true;.
                    appWindow.Closing += async (s, e) =>
                    {
                        e.Cancel = true;                      
                    };
               });         
            });
        });
#endif

Steps to Reproduce

No response

Link to public reproduction project repository

No response

Version with bug

7.0.101

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

macOS

Affected platform versions

No response

Did you find any workaround?

No response

Relevant log output

No response

@AllenMiaoFil AllenMiaoFil added the t/bug Something isn't working label Jan 24, 2024
@dotnet dotnet locked and limited conversation to collaborators Jan 24, 2024
@jfversluis jfversluis converted this issue into discussion #20139 Jan 24, 2024
@samhouts samhouts added the area-core-lifecycle XPlat and Native UIApplicationDelegate/Activity/Window lifecycle events label Jan 25, 2024
@samhouts samhouts added the platform/macOS 🍏 macOS / Mac Catalyst label Feb 2, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
area-core-lifecycle XPlat and Native UIApplicationDelegate/Activity/Window lifecycle events platform/macOS 🍏 macOS / Mac Catalyst t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants