Xamarin Forms on macOS

Xamarin Forms is great for writing cross platform apps and supports Android, iOS and UWP. If you want to use the latest Xamarin Forms source code, you can also develop macOS apps.

Xamarin Forms loves macOS

The wonderful thing with open source software, such as Xamarin Forms, is that you can always try the latest by downloading the source code yourself. Not only that, thanks to the Xamarin Forms team they have also bundled up nightly builds which you can consume from a NuGet feed, details here.

Let’s create a macOS app using Xamarin Forms.

Firstly we need to create a new Xamarin Forms project. We’ll choose a Portable Class Library.

New forms app

Next you will want to add in a Mac Cocoa App. This will add a new project to your solution.

Add Mac project

You will then need to go into the macOS project’s info.plist file and remove the NSMainStoryboardFile key. This is because Xamarin Forms takes care of setting up the main screen.

Remove main storyboard

There are also 2 files that we don’t need. Main.storyboard and ViewController.cs.

Remove files

Remove references to Xamarin.Forms in all projects and add the nightly Xamarin.Forms NuGet packages, detailed here.

Add NSApplication.SharedApplication.Delegate = new AppDelegate(); to Main.cs as below. You will also need to make amendments to AppDelegate.cs as shown below.

using AppKit;
 
namespace FormsDemo.MacOS
{
    static class MainClass
    {
        static void Main(string[] args)
        {
            NSApplication.Init();
            NSApplication.SharedApplication.Delegate = new AppDelegate();
            NSApplication.Main(args);
        }
    }
}
using AppKit;
using Foundation;
using Xamarin.Forms.Platform.MacOS;
 
namespace FormsDemo.MacOS
{
    [Register("AppDelegate")]
    public class AppDelegate : FormsApplicationDelegate
    {
        NSWindow mainWindow;
 
        public AppDelegate()
        {
            var style = NSWindowStyle.Closable | NSWindowStyle.Resizable | NSWindowStyle.Titled;
 
            var rect = new CoreGraphics.CGRect(200, 1000, 400, 600);
            mainWindow = new NSWindow(rect, style, NSBackingStore.Buffered, false);
            mainWindow.Title = "FormsDemo.MacOS";
        }
 
        public override NSWindow MainWindow => mainWindow;
 
        public override void DidFinishLaunching(NSNotification notification)
        {
            global::Xamarin.Forms.Forms.Init();
 
            LoadApplication(new App());
 
            base.DidFinishLaunching(notification);
        }
 
        public override void WillTerminate(NSNotification notification)
        {
            // Insert code here to tear down your application
        }
 
        public override bool ApplicationShouldTerminateAfterLastWindowClosed(NSApplication sender) => true;
    }
}

Once making all the necessary changes you should be able to build and run you macOS app.

Xamarin Forms macOS demo

Keep in mind that Xamarin Forms for macOS is still in the early stages so certain controls or properties on controls may not be implemented or act as expected. Now that it is easier than ever for experienced Xamarin Forms developers to start writing macOS apps I’m sure we’ll start seeing macOS implementations for a lot of the existing Xamarin plugins that are available.

comments powered by Disqus