Preview

It’s amazing to think that you can now have a running native macOS application with just a few tweaks to a Xamarin.Forms solution. And I have to say “application” because that’s desktop, right?

As I featured in last week’s Xamarin.Forms 2.3.5 pre-release announcement, macOS support in Xamarin.Forms is now in Preview. Let’s run through adding this new project to an existing Xamarin.Forms solution, the hurdles you may face, and the current and future state of the platform features.

Configuring a macOS Project

Developing for macOS is a Mac only feature, so the same rules apply as when you are doing a Xamarin.Mac application. There is an excellent Xamarin.Mac guide here that covers the prerequisites.

When the time comes for you to integrate your Xamarin.Forms macOS project into a continuous integration system, rest easy that you can build for macOS on Windows just as you can now for iOS.

Add a Cocoa App Project

Until Xamarin.Forms templates adds a Cocoa App, we can easily add it ourselves. To do that in Xamarin.Studio or Visual Studio for Mac, add a new project to the solution and choose Mac > App > Cocoa App.

Adding macOS Project

Configure the Project

The Cocoa App template is configured with a storyboard implementation, which you won’t need. You may leave it there, but you’ll end up with two windows when your application runs.

Here’s the quick list of what you need to do:

  • Add the Xamarin.Forms 2.3.5.233-pre1 pre-release NuGet
  • In your macOS project, add a reference to your Xamarin.Forms project (shared or PCL)
  • Import any assets such as images or fonts
  • Edit info.plist and remove the source entry (NSMainStoryboardFile)
    info.plist
  • Update Main.cs to initialize the AppDelegate
    static class MainClass
    {
    	static void Main(string[] args)
    	{
    		NSApplication.Init();
    		NSApplication.SharedApplication.Delegate = new AppDelegate();
    		NSApplication.Main(args);
    	}
    }
  • Update AppDelegate to initialize Xamarin.Forms, create a window, and load the Forms App
    • If you have other dependencies that need to be initialized, do that here as well.
using Xamarin.Forms;
using Xamarin.Forms.Platform.MacOS;
...
[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);
	}
}

Run it!

With the macOS project as the startup, run it. Voila! You now have a native macOS application!

Example of Sample Weather App

What’s Next?

Now that you have an app, it’s time to put on your UX Designer hat and start exploring the changes you need to feel more at home in this new desktop environment.

Styling

Take a run at styling your Xamarin.Forms UI for macOS. The same styling used on mobile may not look right. This is easily updated!

With recent changes made to OnPlatform you can now target any number of platforms. That includes macOS.

<Button.TextColor>
	<OnPlatform x:TypeArguments="Color">
		<On Platform="iOS" Value="White"/>
		<On Platform="macOS" Value="White"/>
		<On Platform="Android" Value="Black"/>
	</OnPlatform>
</Button.TextColor>

Note you may also double up on platforms like <On Platform="iOS, macOS" ...>.

Window Size and Position

Is the launch window too big? Too small? Launching in an odd position on screen? You can adjust all of this in the AppDelegate on this line:

var rect = new CoreGraphics.CGRect(200, 1000, 1024, 768);

Known Issues

This is a Preview, so you should expect that not everything is production ready. Below are a few things you may encounter as you add macOS to your projects.

Not All NuGets are Ready for macOS

In order to work in a macOS project, packages must target xamarinmac20. You may find that some of your beloved libraries do not yet support macOS. What can you do? Kindly send a request to the project’s maintainer to add it. Until they have support, you may need to look for alternatives.

Xamarin.Forms Features

This preview has amazing coverage of Xamarin.Forms UI and features, but there are some known gaps to be aware of.

Not Yet Implemented:

  • OpenGlRenderer
  • ListView – ScrollTo, UnevenRows support, refreshing, SeparatorColor, SeparatorVisibility, Footer
  • ViewCell – IsEnabled, ForceUpdateSize
  • Image – Aspect
  • Picker – Bindable / Observable implementation
  • MasterDetailPage – BackgroundColor
  • TabbedPage – BarBackgroundColor, BarTextColor
  • TableView – UnevenRows
  • WebView – Most WebNavigationEvents
  • Navigation – InsertPageBefore

Send Your Feedback!

Add a macOS project and the Xamarin.Forms 2.3.5.233-pre1 pre-release to your solutions today and let us know what you think. What do you feel is missing? What problems do you encounter?

For a quick start, you can grab the WeatherApp I updated on GitHub: https://github.com/xamarin/xamarin-forms-samples/tree/forms-macos-preview/Weather

Join the macOS discussion in the Xamarin forums.