Creating a MAC Application Using Xamarin.Forms

Introduction 

This blog post will explain how we can create MAC applications using the Xamarin.Forms App.
 
Let's start by adding a Cocoa project in our existing xamarin.Forms Project.
 
a
  1. Open Visual Studio
  2. Click On new project.
  3. Click On App under the MAC section as shown in the image above.
  4. Add Cocoa Project.
  5. Now Install Xamarin.Forms Package in your cocoa Application making sure it has the same version other projects are referring to.
  6. Add a reference to your PCL project to your cocoa project.
  7. Open Info.Plist in textEditor and Remove the source entry NSMainStoryboardFile.
  8. Now Open the AppDelegate class of your cocoa application.
  9. Change the AppDelegate derived from NSApplicationDelegate to Xamarin.Forms.Platform.MacOS.FormsApplicationDelegate.
  10. Go to DidFinishLaunching function of app Delegate and add below 2 lines   
  1. Xamarin.Forms.Forms.Init();// Intitilise our xamarin.Forms Project.    
  2. LoadApplication(new App());// refer to App from Cocoa project.   
So now our DidFinishLaunching looks like 
  1. public override void DidFinishLaunching(NSNotification notification)  
  2. {  
  3.    Xamarin.Forms.Forms.Init();  
  4.    LoadApplication(new App());  
  5.    base.DidFinishLaunching(notification);  
  6. }  
In AppDelegate constructor we need to create a new window which will be returned from main window property of AppDelegate()
  1. public AppDelegate()  
  2. {  
  3.    var style = NSWindowStyle.Closable | NSWindowStyle.Resizable | NSWindowStyle.Titled;  
  4.    var rect = new CoreGraphics.CGRect(200, 1000, 1024, 768);  
  5.    window = new NSWindow(rect, style, NSBackingStore.Buffered, false);  
  6.    window.Title = "Xamarin.Forms on Mac!";  
  7.    window.TitleVisibility = NSWindowTitleVisibility.Visible; //This will be the title of the app if we dont want anything to be shown it will look like  
  8.    //window.TitleVisibility = NSWindowTitleVisibility.Hidden;  
  9. }  
Goto Main.cs and add 
 
NSApplication.SharedApplication.Delegate = new AppDelegate();  
 
Main.cs Looks Like.
 
static void Main(string[] args)
{
NSApplication.Init();
NSApplication.SharedApplication.Delegate = new AppDelegate(); // add this line
NSApplication.Main(args);
}
 
 
Now we just need to set our cocoa application as a startup project and run it. The output should look like:
 
So we have successfully created and executed our first Xamarin.Forms MAC Application.
 
Happy Coding :)