Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin.Forms targeting macOS

I am using Visual Studio for Mac 7.6.6 to create a Xamarin.Forms app targeting macOS (to be shared with something running on Windows). I create a new Project and select

  1. Multiplatform App | Blank Forms App. Click Next
  2. Configure your Blank Forms App. There are target platforms: Android and iOS. (There is nothing for macOS). Since I have not installed the build toolkits for either iOS and android, both of these checkboxes are disabled. Therefore the Next button on this wizard page is disabled.

How do I proceed? I assume there is no way to use the New Project wizard for this.

I came across an old post for starting with a Xamarin Cocoa app and using NuGet to put the Xamarin Forms functionality but don't understand the code

LoadApplication(new App());  // <-- don't know what App is

I suspect the VS Mac and Xamarin.Forms are out of sync being on the bleeding edge. Has anyone gotten this to work?

like image 873
David Ching Avatar asked Dec 17 '22 20:12

David Ching


2 Answers

I would suggest following SushiHangover's suggestion since that is simpler which is what you have done already:

  1. Add a new CocoaApp project to your solution.
  2. Install Xamarin.Forms NuGet package into your CocoaApp.
  3. Reference the shared project or .NET Standard project from your CocoaApp project.
  4. Edit info.plist and remove the source entry (NSMainStoryboardFile).
  5. Change the AppDelegate to derive from Xamarin.Forms.Platform.MacOS.FormsApplicationDelegate.
  6. Update Main.cs to initialize the AppDelegate
  7. In the AppDelegate's DidFinishLaunching add the code to initialize Xamarin.Forms.
  8. Create a new NSWindow which should be returned from the MainWindow property in the AppDelegate.

Main.cs:

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

AppDelegate.cs:

[Register("AppDelegate")]
public class AppDelegate : Xamarin.Forms.Platform.MacOS.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)
    {
        Xamarin.Forms.Forms.Init();
        LoadApplication(new App());

        base.DidFinishLaunching(notification);
    }

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

However Visual Studio for Mac does include a Mac project with the Xamarin.Forms project templates. However it does not expose this in the New Project dialog currently. You can create a Mac Forms project from this template but it is a bit more work than what SushiHangover suggested and you have used.

  1. Install the Xamarin.Forms project template into the .NET Core project templates

    dotnet new --install "/Applications/Visual Studio.app/Contents/Resources/lib/monodevelop/AddIns/Xamarin.Forms.Addin/Templates/Xamarin.Templates.Multiplatform.0.0.1.nupkg"

  2. Create a new Forms project including the Mac project (you may want to review and set other template parameters - the following creates a blank Forms app with Android, iOS, Mac, UWP and a Shared project).

    dotnet new forms-app --CreateMacProject -k blank

  3. Create a new blank solution (Other - Miscellaneous - Blank Solution) in the parent directory of the projects you just created.

  4. Add all those projects created to the solution.

Then you can build and run the Mac project which includes Xamarin.Forms.

Note you may want to remove the Xamarin.Forms project template from the .NET Core project templates which you can do by running the following:

dotnet new --debug:reinit
like image 172
Matt Ward Avatar answered Jan 18 '23 01:01

Matt Ward


In addition to the approved answer, which is incomplete with recent updates, now you have to do one more step. Link the delagate in the Mac project main class

main.cs:

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

nota. My edit was refused and I'm not allowed to add comments. So I added a complementary answer to help those looking for help now.

like image 44
Francisl Avatar answered Jan 17 '23 23:01

Francisl