Deserializing JSON into a Dynamic Object

Deserializing in JSON.NET can be dynamic using the JObject class, which is included in that library. My JSON string represents these classes:
A normal conversion to string and back can be done easily:
Now we deserialize WITHOUT referencing the Foo class directly:
Or if you want to go deeper:

Read More

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

Conditional Ordering and Subsorting in LINQ

To accomplish conditional ordering you build upon your original LINQ assignment variable (in this case recs). If there are any records that haven’t been viewed by a user, the set is sorted one way, otherwise another.
As for subsorting notice the ThenByDescending method. I first sort ascending by a boolean that pushes all the false values to the top. I then subsort descendingly by a date. This will isolate the false group and subsort that group by a descending date. It will do the same with the true group.

Read More

Assign ItemsSource to a Responsive Variable

The ObservableCollection class is nice for updating interface collections (such as dropdowns and listboxes) on the fly. Initialize the CList variable publicly. After that you can adjust that variable in any event and see immediate results.

The following example adds items to a combobox.
Now adjust the variable and see the GUI combobox change instantly.

Read More

Named Arguments

This may seem trivial but I just recently came across this gem. Named Arguments allow you to specify which argument a value belongs to in a method call. One of its advantages is that it frees you from looking up the order of parameters in the call. Meh! That may be cool for some but I find myself around knowing that answer from intellisense while typing. But an application that is useful is if you have a slew of optional parameters and you only need to adjust one or more further down the line.

The following is a traditional way in which you would adjust a distant optional parameter. You would have to supply all the argument values that proceed it:

Now using named arguments you can call it as follows:

It simplifies the call and makes it easier to know the meaning of each argument at a glance. There have been times in the past where I have used Overloading to bridge the argument gap. Now with Named Arguments, I have another tool in my arsenal to supply arguments in a precise and intelligible way.

Read More