Customize NSComboBox Items in Xamarin.Mac

Have you ever wanted to change the color or fontweight of an item in a NSComboBox? Making an item red or bold can give attention to options you want the client to choose.

In this example I will be populating the NSComboBox I declared in Xcode via a data source. The ObjectValueForItem method is where we do our item customizations:
Lastly, assign the data source when the window loads

Read More

Balloon Tip Alternative in Xamarin.Mac

In Windows, a system tray application can popup a message for the user above the icon without interaction. This is commonly referred to as a balloon tip notification or a balloon tooltip. NSUserNotification exampleThis kind of functionality seems to be absent from Mac. Not only that, if you did accomplish something similar it would be unconventional. When making applications on different platforms you must decide what features should be identical and what should be comparable. In Mac the equivalent effect is a NSUserNotification. The messages slides out from the top-right of the screen, not from underneath an icon. This class was introduced in OSX v10.8, so if you must support older systems then look into the Growl framework.

The below example shows how it is done. The message is formed, events handlers are set and the notification is shown:
I experienced trouble at first getting this to work. But my error had nothing to do with the code. I was using VNC on my Windows machine to interact with a Mac laptop. The Mac lid was closed. It wasn’t till I opened the lid that the notifications started appearing. Notification settings can be tweaked in System Preferences under Notifications

Read More

Click Event of NSStatusItem in Xamarin.Mac

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 NSMenuItem in 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 Action property of the NSStatusItem. It is more like a Click or Activated event. 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 Menu property at runtime negates the Click event. 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.

I accomplished this with the PopUpStatusItemMenu method.
Variables explained:
  • statusItemNSStatusItem
  • menuAuthStatusNSMenuItem
  • menuAuthNSMenu
  • 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!

Read More

Cancel/Abort a Closing Window in Xamarin.Mac

Close Dialog

I began by researching the WillClose event to accomplish this. With it I could detect the window as it was closing but could not find a way to abort with the EventArgs provided. I achieved success with a delegate class that overrides the WindowShouldClose method .
Put this class in the window.cs below the partial class that was pregenerated:

Assign the delegate wherever you instantiate the window controller:

Read More

Center a Window in Xamarin.Mac

To make my window appear front, centered and focused I have had success with the following:

This works exceptionally well when invoked from a NSStatusBarItem.

Read More