Status bar menu for Mac Catalyst in .NET MAUI

960 views Asked by At

I have been working on an .NET MAUI app that needs to have a status bar icon and when clicked, menu to be shown. I know it is a platform specific issue and I have been folowing this tutorial from Microsoft. They have image button implemented to be shown in the status menu bar in the TrayService.

This runtime binding and creating objects are diffucult to me. The idea is to create NSMenu object, assign it to the NSStatusBar object, then create NSMenu Items with appropriate click events and assign them to the NSMenu object.

I am struggling since I don't know how to create the NSMenu object, and NSMenuItem objects with Title property for example.

NSMenu have many constructors, properties, methods to use and I don't know how to use them.

I will be very greatful with explanation or example on how to start.

_statusBarMenu = Runtime.GetNSObject(Class.GetHandle("NSMenu"));
IntPtr_objc_msgSend_String(_statusBarMenu.Handle, Selector.GetHandle("initWithTitle:"), "Quit");
void_objc_msgSend_IntPtr(_statusBarItem.Handle, Selector.GetHandle("setMenu:"), _statusBarMenu.Handle);

This compiles fine, but I don't know if IntPtr_objc_msgSend_String method exist because I added it myself at the top. However, nothing shows up in the tray.

1

There are 1 answers

2
Zack On

It should be noted that Mac Catalyst is used in Maui, NSMenu and NSMenuItem are in Macos, and are not supported in Maui. For the Mac Catalyst creation menu in Maui you can refer to:

Menubar

Adding Menus and Shortcuts to the Menu Bar and User Interface | Apple Developer