Shaun Hevey
Targeting macOS with Xamarin.Forms
February 25, 2020
By Shaun Hevey

If you haven't developed anything with Xamarin.Forms before it is a UI framework for developing cross platform user interfaces on top of Xamarin. Out of the box it has official support for targeting iOS, Android & UWP (Windows 10 Universal Windows Platform), although it does support a number of other platforms these are still in preview this means not all features have been implemented and at the time of writing macOS is one of these platforms. If you would like you can read all about the platforms that Xamarin.Forms support check out the following link.

Let's take a look at how you can add macOS as a target for your Xamarin.Forms application. I will be using Visual Studio for Mac as you won't be able to add this platform using Visual Studio on windows.

Add Mac project

Once you have opened your Xamarin.Forms application in Visual Studio for Mac you need to do add a Mac project.

  1. Right-click on Xamarin.Forms solution and choosing Add -> Add New Project...
  2. In the window that pops up, scroll down the template list scroll down to Mac section and choose App, then select to the Cocoa App project template and click Next.
  3. Enter your App Name and Dock item if you want to change the name that appears in the dock, then click Next.
  4. Make sure you are happy with the configuration on the next page and select Create.

Note see video below to see these steps in action.

Add Mac project to Xamarin.Forms solution

Add dependencies to Mac project

Let's add the Xamarin.Forms NuGet package.

  1. Right click on the Packages folder in the Mac project, then select Manage NuGet Packages...
  2. In the window that appears install the Xamarin.Forms NuGet package. Make sure all the other projects are updated to use the same version of the Xaramin.Forms package.

Show NuGet package to add

Now it is time to add the required reference to the Mac project.

  1. Right click on the References folder in the Mac project.
  2. In the references window Select the Projects tab and select the Xamarin.Forms project (this will either be a Shared Project or .NET Standard library).

Adding project reference to Mac project

Time to add some code

There is couple of files that need to be updated to initialise the Xamarin.Forms in the Mac project.

Main.cs

In Main.cs you need to add the following line in the Main method below NSApplication.Init();.

NSApplication.SharedApplication.Delegate = new AppDelegate();

Your code should like this.

static class MainClass
{
static void Main(string[] args)
{
NSApplication.Init();
NSApplication.SharedApplication.Delegate = new AppDelegate();
NSApplication.Main(args);
}
}

AppDelegate.cs

The AppDelegate needs a little more change than Main.cs. Add the following usings to the top of your class.

using Xamarin.Forms;
using Xamarin.Forms.Platform.MacOS;
// also add a using for the Xamarin.Forms project

Next you need to change AppDelegate from inheriting from ApplicationDelegate to FormsApplicationDelegate

public class AppDelegate : FormsApplicationDelegate

A new NSWindow object needs to be added and then initialised in this class so add the following code. Change the ctor code to personalise the window as required for your application.

NSWindow window;

public AppDelegate()
{
var style = NSWindowStyle.Closable | NSWindowStyle.Resizable | NSWindowStyle.Titled;

var rect = new CoreGraphics.CGRect(200, 1000, 1024, 768);
window = new NSWindow(rect, style, NSBackingStore.Buffered, false);
window.Title = "Xamarin.Forms on Mac!";
window.TitleVisibility = NSWindowTitleVisibility.Hidden;
}

public override NSWindow MainWindow
{
get { return window; }
}

Now the window has been created you need to initialise Xamarin.Forms. To do this you need to add or update the follow method.

public override void DidFinishLaunching(NSNotification notification)
{
Forms.Init();
LoadApplication(new App());
base.DidFinishLaunching(notification);
}

If you have following the above steps your AppDelegate should look like the following.


using Xamarin.Forms;
using Xamarin.Forms.Platform.MacOS;
// also add a using for the Xamarin.Forms project, if the namespace is different to this file
...
[Register("AppDelegate")]
public class AppDelegate : FormsApplicationDelegate
{
NSWindow window;
public AppDelegate()
{
var style = NSWindowStyle.Closable | NSWindowStyle.Resizable | NSWindowStyle.Titled;

var rect = new CoreGraphics.CGRect(200, 1000, 1024, 768);
window = new NSWindow(rect, style, NSBackingStore.Buffered, false);
window.Title = "Xamarin.Forms on Mac!"; // choose your own Title here
window.TitleVisibility = NSWindowTitleVisibility.Hidden;
}

public override NSWindow MainWindow
{
get { return window; }
}

public override void DidFinishLaunching(NSNotification notification)
{
Forms.Init();
LoadApplication(new App());
base.DidFinishLaunching(notification);
}
}

The last thing you need to do is remove the reference to the default storyboard.

  1. Open Info.plist and select the Application tab in the editor.
  2. Clear out Main Interface box to remove the reference to Main.storyboard.

Removing the refence to Main

Note you can now delete Main.storyboard if you would like.

Resources

You will now need to add any references e.g image files from your existing platforms that you application requires to run.

If all of this has worked you should now be able to run your Xamarin.Forms Mac application, as shown below.

Mac application running