2017/12/31

Xamarin.Forms on OSX

Not sure how I missed this, but Xamarin.Forms now has preview support for Mac OSX clients.  This is really exciting and I hope they decide to finalize support for OSX as a first-class target.

I spend fair amount of time on OSX, so one of my biggest gripes about Forms was lack of support for a native Mac client.  On Windows I can target UWP and run projects directly on Windows 10.  But, on OSX I have to launch either the iOS or Android target.  Not a deal breaker, but certainly cumbersome.

MvvmCross promises to be a Xamarin.Forms-like MVVM framework that targets more platforms.  But, it seems OSX clients are languishing as a low-priority concern for that project.

Working Client

The xamarin blog's instructions mostly worked.  I added a Cocoa app with the latest version of Xamarin.Forms on nuget (2.5.0) to my Forms solution, and made the required changes.

Everything built, but when I launched it I got a System.NullReferenceException:
1:  Xamarin.Forms.Platform.MacOS.FormsApplicationDelegate.UpdateMainMenu() in   
2:  Xamarin.Forms.Platform.MacOS.FormsApplicationDelegate.DidFinishLaunching(Foundation.NSNotification notification) in   
3:  mobile_app.OSX.AppDelegate.DidFinishLaunching(Foundation.NSNotification notification) in /Users/jake/jade/jade/mobile_app/mobile_app.OSX/AppDelegate.cs:33  
4:  AppKit.NSApplication.NSApplicationMain() in   
5:  AppKit.NSApplication.Main(string[] args) in /Users/builder/data/lanes/5489/c4240f3f/source/xamarin-macios/src/AppKit/NSApplication.cs:100  
6:  mobile_app.OSX.MainClass.Main(string[] args) in /Users/jake/jade/jade/mobile_app/mobile_app.OSX/Main.cs:12  

I found the solution in the xamarin forums.  Need to change the initialization of NSApplication in Main.cs:
1:  static void Main(string[] args)  
2:  {  
3:    NSApplication.Init();  
4:    NSApplication.SharedApplication.Delegate = new AppDelegate();  
5:    NSApplication.SharedApplication.MainMenu = new NSMenu();  // FIX  
6:    NSApplication.Main(args);  
7:  }  
Line 5 is the added line.

Giving me a working- albeit embarrassingly trivial- app:

No comments: