This issue annoyed me a bit so I thought I’d share. I was working with an application that had a
NSStatusItem(also known as a Menu Extra). This is the Windows equivalent of a system tray application. My goal was to show the status of my application in the top item of its context menu. I had a variable in a shared project that’s updated with a status. I struggled to find a way to bind this variable to the
NSMenuItemin a slick way. Then it occurred to me that I could update it as the menu opens. I needed to find an event equivalent to opening/showing/expanding. I came across an example online using the
Actionproperty of the
NSStatusItem. It is more like a
Activatedevent. So that’s what I used.
For some reason I struggled to get it to fire. I eventually came across this Apple documentation. It says setting the
Menuproperty at runtime negates the
Clickevent. So I had to find a way to assign the menu in a different way to keep the event in tact. Whatever I found must be dynamic.
//in my AppDelegate.cs
public override void DidFinishLaunching (NSNotification notification)
statusItem = NSStatusBar.SystemStatusBar.CreateStatusItem (30);
statusItem.Image = NSImage.ImageNamed ("menuicon");
//statusItem.Menu = menuAuth;
statusItem.Action = new ObjCRuntime.Selector ("MenuClick");
statusItem.Target = this;
I accomplished this with the
private void MenuClick ()
menuAuthStatus.Title = Client.TrayStatus;
Client.TrayStatus– the string variable in a shared project
Now each time the icon is clicked the item text of the menu is immediately set to the shared variable. Then the menu is presented to the user in all its glory!