February 25, 2020
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.
Once you have opened your Xamarin.Forms application in Visual Studio for Mac you need to do add a Mac project.
Note see video below to see these steps in action.
Let's add the Xamarin.Forms NuGet package.
Now it is time to add the required reference to the Mac project.
There is couple of files that need to be updated to initialise the Xamarin.Forms in the Mac project.
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);
}
}
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.
Note you can now delete Main.storyboard if you would like.
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.