Getting Started with Xamarin Forms for Mac Preview

Getting Started with Xamarin Forms for Mac Preview

Last month David Ortinau of Xamarin.Forms team published this announcement on Xamarin Forum that, they have released the preview version of Xamarin.Forms for Mac. I was thinking about giving it a try since I came to know about the source tree last year and even tried to setup the code on my local ( even blogged about it under a different topic).

So today I decided to extend my last week’s SkiaSharp example to Xamarin.Forms for Mac.

The same steps you can follow to extend any of your other Xamarin forms application in Mac (Provided you have a Mac Machine 🙂 ) . So let’s get started:

  1. Since the last project we had created in Windows, we have to setup the code on our Mac machine, for which you have to go the Github and copy the Cloning Url like below image:
  2. Open Visual Studio For Mac –> click on Version Control Menu –> click on Checkout.. option, it will open the Connect To Repository window where you have to fill the URL (from previous step), set the location where you want to setup the application code and click on Checkout button to download the code and load it in Visual Studio for Mac:
  3. Once the source code is downloaded on disk, loaded in Visual Studio for Mac the solution will appear like this :
  4. Build the code and execute any of the iOS or Android project just to check that the application is working fine. I checked with Android and it was work like last time 🙂
  5. We have followed the above steps because I wanted to extend my existing application to Support for Xamarin.Forms for Mac. These are not needed when you create a new Xamarin.Forms application on your Mac itself.
    Next Steps which we are going to follow are the main steps used to extend any existing Xamarin.Forms application with Xamarin.Forms Mac support.

  6. Right Click on the Solution and Click on Add New Project from the pop-up menu. It will open Choose a template for your new project window. There select Mac–>App, General–> Coca App and click Next as shown in below image:
  7. Then Configure your Mac App window will appear where you have to provide the App Name , Organisation Identifier, Dock Item (if you want to use different name like me) and click on Next again shown in below image:
  8. Now the last screen of wizard will appear where you have to provide the Project Name and Location of the project. click on Create after that, as highlighted in below image:
  9. Once new project is created we need to add Xamarin.Forms Nuget Package. To do so Right Click on the project and Click on Add –> Add Nuget Packages… which will open Add Packages window. There check the Show pre-release packages check box then Search and select Xamarin Forms like below Image :
  10. Since We are extending our SkiSharp example so we need to install the SkiaSharp Nuget Package too. For that follow the same steps however this time uncheck the Show pre-release packages check box as shown in below image:
  11. Now since we have to use the UI code written in our common PCL library, so add it’s reference. For that Right Click on References folder of Project and Click on Edit References option which will Open Edit References Window, There Click on Projects Tab and select PCL project ( SkiaSharpEx in our case) and click on ok as highlighted in below image:
  12. Since the UI of the application will be created using Xamarin.Forms, we don’t require the Storyboard created by default in project template. To remove that open info.plist file –> Source Tab remove NSMainStoryboardFile entry :
  13. All the configuration changes finished finally 🙂 now lets start with the code changes which are very few. Firstly change the Main method of Main.cs file with following code :
            static void Main(string[] args)
            {
    			NSApplication.Init();
    			NSApplication.SharedApplication.Delegate = new AppDelegate();
    			NSApplication.Main(args);
            }
    

    This code will initialise the AppDelegate for the app.

  14. Update the code of AppDelegate.cs with following code:

    using AppKit;
    using Foundation;
    using Xamarin.Forms;
    using Xamarin.Forms.Platform.MacOS;
    
    namespace SkiaSharpEx.Mac
    {
        [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!";
    			_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);
    		}
        }
    }
    

    Here we are inheriting the default Appdelegate from the new FormsApplicationDelegate, Initializing a new Window and loading the Xamarin.Forms App in that Windows.

  15. All the configuration and coding done 🙂 time to execute the code, set the SkiaSharp.Mac project as Startup project and execute it. You will get following output :

    This is expected as we had created the image with fixed values, which changes on platform. You can play around with the updated code of Github to correct the image 🙂

As Xamarin.Forms for Mac is in Preview, there may be some issues in the Apps or some features missing but it’s a great way for existing Xamarin.Forms developers to extend their apps for Mac Desktops and publish on Mac AppStore.

Let me know if I have missed anything or need any other example.

🙂 🙂 🙂 🙂 Happy Coding 🙂 🙂 🙂 🙂

One thought on “Getting Started with Xamarin Forms for Mac Preview

Leave a Reply

Your email address will not be published. Required fields are marked *