macosbackgroundmauisystem-traymac-catalyst

Run MAUI app with Mac Catalyst under MacOS in system tray (Background)


I'm trying to write a MAUI app that runs in the SystemTray. I managed to do this on Windows. Unfortunately I can't figure out how to get this to work under MacOS.

I created the system tray based on this model: WeatherTwentyOne

My solution, so that the application under Windows continues to run in the system tray after it is closed:

        public App()
        {
            InitializeComponent();
            MainPage = new AppShell();
        }

        protected override Window CreateWindow(IActivationState activationState)
        {
            Window window = base.CreateWindow(activationState);

#if WINDOWS
            window.Created += (s, e) =>
            {
                var handle = WinRT.Interop.WindowNative.GetWindowHandle(window.Handler.PlatformView);
                var id = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(handle);
                appWindow = Microsoft.UI.Windowing.AppWindow.GetFromWindowId(id);
                appWindow.Closing += (s, e) =>
                {
                    e.Cancel = true;
                    appWindow.Hide();
                };
            };
        }

Unfortunately, I can't find any information that will help me.


Solution

  • On the Mac Apps, you could create a new NSStatusItem on the right-top to make it look like the system tray on Windows mashine. And NSStatusBar.SystemStatusBar needs to be configured in the DidFinishLaunching method of AppDelegate.

    The key is that NSStatusBar only supports MacOS 10.0+(please see Apple's NSStatusBar Doc).

    MAUI Apps for the Mac platform use MacCatalyst (please see MAUI supported platforms). Since this feature is not supported with MacCatalyst API by Apple, you couldn't achive this functionality through MAUI.