[macOS] Multi-window app does not terminate #13574
-
DescriptionWhen I enable multi-window support for MAUI on MacCatalyst according to official instructions, the application does not terminate when the app's "X" button is clicked. Steps to ReproduceCheck out the commit MartyIX/macOS-MAUI-multiwindow@7b4861c (vanilla MAUI application):
Check out the latest commit of https://github.com/MartyIX/macOS-MAUI-multiwindow (MartyIX/macOS-MAUI-multiwindow@d2ed4ac) that adds the multi-window support:
Link to public reproduction project repositoryhttps://github.com/MartyIX/macOS-MAUI-multiwindow Version with bug7.0 (current) Last version that worked wellUnknown/Other Affected platformsmacOS Affected platform versionsmacOS Ventura 13.2.1 Did you find any workaround?No. Relevant log outputNo response |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 2 replies
-
Usually in .net apps u have to create the close code the apps dont no how to other wise something like Application.Terminate? |
Beta Was this translation helpful? Give feedback.
-
Answer from @drasticactions:
So to put it simply: You close the last window but that does not automatically means that the application is stopped (as is customary on Windows and Linux). Anyway, I tried to modify my using System.Diagnostics.CodeAnalysis;
using Foundation;
using Microsoft.Maui;
using Microsoft.Maui.Hosting;
using UIKit;
namespace macOS_maui_multiwindow;
[Register("AppDelegate")]
public class AppDelegate : MauiUIApplicationDelegate
{
+ /// <summary>
+ /// Returns a boolean value that indicates if the app terminates once the last window closes.
+ /// </summary>
+ /// <param name="application">Application object whose last window was closed.</param>
+ /// <returns>Always <c>true</c>.</returns>
+ /// <seealso href="https://developer.apple.com/documentation/appkit/nsapplicationdelegate/1428381-applicationshouldterminateafterl"/>
+ [Export("applicationShouldTerminateAfterLastWindowClosed:")]
+ public virtual bool ApplicationShouldTerminateAfterLastWindowClosed(UIApplication application)
+ {
+ return true;
+ }
/// <inheritdoc/>
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
} but unfortunately, it's not called because this API works for AppKit and not for UIKit. So this won't work. Next action is to try: https://github.com/drasticactions/Drastic.Tools/blob/e141a5785537021890a26390c970c40239ae703d/src/Drastic.TrayWindow/NSApplication.Catalyst.cs#L39-L47 Resources
|
Beta Was this translation helpful? Give feedback.
-
This issue is not MAUI Specific; this is how Mac Catalyst generally works. With a single instance application, closing the window closes the application. When you enable multiple windows, the opposite happens; Closing all of the open windows does not close the application but keeps it running so you can open windows from the dock. As I pointed out, you can change that behavior by accessing the underlying AppKit Delegate and forcing it to change. Still, these are private APIs and are discouraged from use. I am moving this to a discussion as this is not a MAUI bug. |
Beta Was this translation helpful? Give feedback.
-
ApplicationShouldTerminateAfterLastWindowClosed should be exposed by MauiUIApplicationDelegate to be overridden in AppDelegate, so we can use its o handle the closing the app via red button. We were able to override it in Xamarin.MacOS's AppDelegate. |
Beta Was this translation helpful? Give feedback.
-
you can handle window destroying for each window you create, but it is not best solution, although only possible right now Application window
|
Beta Was this translation helpful? Give feedback.
This issue is not MAUI Specific; this is how Mac Catalyst generally works. With a single instance application, closing the window closes the application. When you enable multiple windows, the opposite happens; Closing all of the open windows does not close the application but keeps it running so you can open windows from the dock.
As I pointed out, you can change that behavior by accessing the underlying AppKit Delegate and forcing it to change. Still, these are private APIs and are discouraged from use.
I am moving this to a discussion as this is not a MAUI bug.