Add a macOS project to your existing solution

With the new version of Xamarin, we can create apps for macOS. But how can I add a macOS project to my solution? I explain step by step what we have to do.

Add new project

The first step is to add a new project for macOS in your solution. Right-click on your solution and select Add New project... Step 1 - Add new project

Now select from the list App under Mac and then Cocoa App.Step 2 - Select project type

Then name your project, usually, we call this project with yourprojectname.macOS. Step 3 - Name your project

How you see in the preview, the name in the dock it is not very nice. Then we can change it: check Use a Different App Name in Dock and then type the app name.Step 4 - Change name for dock

At the end select the folder where you want to save your project, usually, in the root of your folder in the filesystem.Step 5 - Select project folder

Add packages

In your new project add under Packages Xamarin.Forms and all your other packages you are using in the other project.

Now we have to add the main project. Under References, select the main project and the other dependencies. Edit references

Update the code

Now the complicated part is starting. In your Info.plist file, we have to remove NSMainStoryboardFile.Edit Info.plist

Now in your macOS project, we have to delete Main.storyboard because we don't need it anymore. Right-click on Main.storyboard and then select Remove.

Main.storyboard in your projectMain.storyboard delete

Open your Main.cs and change the Main function with the following code:

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

Open AppDelegate.cs and add at the top the following packages:

using Xamarin.Forms;
using Xamarin.Forms.Platform.MacOS;

You see now AppDelegate derives from NSApplicationDelegate. Change NSApplicationDelegate with FormsApplicationDelegate. Now you see that AppDelegate is underlined with a red line. We take care of that shortly.

Now add in the AppDelegate body 

NSWindow window;

We override MainWindow function (Visual Studio helps to create this function with the correct signature). As return, we use window.

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

Now we have to display a window. For that we have to change AppDelegate(). For creating a window, we have to define its style and dimension.

public AppDelegate()
{
    var style = NSWindowStyle.Closable | NSWindowStyle.Resizable | 
                NSWindowStyle.Titled;
    var rect = new CoreGraphics.CGRect(100, 100, 1024, 768);

    window = new NSWindow(rect, style, NSBackingStore.Buffered, false);
    window.Title = "WordBank Easy";
    window.TitleVisibility = NSWindowTitleVisibility.Hidden;
}

The last thing to do, it is the inizialization of Xamarin.Forms and your app. We change now DidFinishLaunching

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

    base.DidFinishLaunching(notification);
}

Pretty easy, isn't it? Here my complete code of AppDelegate.cs

using AppKit;
using Foundation;
using Xamarin.Forms;
using Xamarin.Forms.Platform.MacOS;

namespace WordBankEasy.macOS
{
    [Register("AppDelegate")]
    public class AppDelegate : FormsApplicationDelegate
    {
        NSWindow window;

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

        public AppDelegate()
        {
            var style = NSWindowStyle.Closable | 
                        NSWindowStyle.Resizable | 
                        NSWindowStyle.Titled;
            var rect = new CoreGraphics.CGRect(100, 100, 1024, 768);

            window = new NSWindow(rect, style, NSBackingStore.Buffered, 
                                  false);
            window.Title = "WordBank Easy";
            window.TitleVisibility = NSWindowTitleVisibility.Hidden;
        }

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

            base.DidFinishLaunching(notification);
        }

        public override void WillTerminate(NSNotification notification)
        {
            // Insert code here to tear down your application
        }
    }
}

Happy coding!

Add comment

Advertsing

125X125_06

TagCloud

MonthList

CommentList