Targeting macOS with Xamarin.Forms

by Shaun


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();.

1NSApplication.SharedApplication.Delegate = new AppDelegate();

Your code should like this.

1static class MainClass
2{
3 static void Main(string[] args)
4 {
5 NSApplication.Init();
6 NSApplication.SharedApplication.Delegate = new AppDelegate();
7 NSApplication.Main(args);
8 }
9}

AppDelegate.cs

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

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

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

1public 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.

1NSWindow window;
2
3public AppDelegate()
4{
5 var style = NSWindowStyle.Closable | NSWindowStyle.Resizable | NSWindowStyle.Titled;
6
7 var rect = new CoreGraphics.CGRect(200, 1000, 1024, 768);
8 window = new NSWindow(rect, style, NSBackingStore.Buffered, false);
9 window.Title = "Xamarin.Forms on Mac!";
10 window.TitleVisibility = NSWindowTitleVisibility.Hidden;
11}
12
13public override NSWindow MainWindow
14 {
15 get { return window; }
16}

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

1public override void DidFinishLaunching(NSNotification notification)
2{
3 Forms.Init();
4 LoadApplication(new App());
5 base.DidFinishLaunching(notification);
6}

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

1using Xamarin.Forms;
2using Xamarin.Forms.Platform.MacOS;
3// also add a using for the Xamarin.Forms project, if the namespace is different to this file
4...
5[Register("AppDelegate")]
6public class AppDelegate : FormsApplicationDelegate
7{
8 NSWindow window;
9 public AppDelegate()
10 {
11 var style = NSWindowStyle.Closable | NSWindowStyle.Resizable | NSWindowStyle.Titled;
12
13 var rect = new CoreGraphics.CGRect(200, 1000, 1024, 768);
14 window = new NSWindow(rect, style, NSBackingStore.Buffered, false);
15 window.Title = "Xamarin.Forms on Mac!"; // choose your own Title here
16 window.TitleVisibility = NSWindowTitleVisibility.Hidden;
17 }
18
19 public override NSWindow MainWindow
20 {
21 get { return window; }
22 }
23
24 public override void DidFinishLaunching(NSNotification notification)
25 {
26 Forms.Init();
27 LoadApplication(new App());
28 base.DidFinishLaunching(notification);
29 }
30}

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