Getting Started with C#

Of course to follow along you’ll need to download and install VS for macOS

Once you have it you can easily start a new “solution”
This is MS’ term for what might roughly be the equivalent of a Xojo project (although its a bit more than that) But for now “Xojo project” is close enough.

Select “New”

Select Mac app in the left hand pane send select “Cocoa app” and make sure the language elector is set to C# (you can use others supported by C# but I’m going to focus on C#)

Press Continue

Give your project a name – I used getting_started

Note that in this pane you can select which version of macOS you want to deploy backwards to. Leave it set to macOS 10.14 for now.

Since we’re going to use a different overall framework we arent interested in a document based app either.

Press Continue

Note that GIT integration is built right into the IDE.

Save the project in whatever location you like.

Press Create and Visual Studio will do its thing to create everything needed.

Once it has you should see a screen something like the following.

Now if you wanted you could write a traditional macOS app in C# using the entire macOS framework (which oddly enough MS has done a decent job documenting)

However, the whole point of this tutorial is to use an alternate more “Xojo like framework”

Visual Studio has a nice package management system called nuget built right in. This makes it easy to add in various “libraries” other authors have made available and keep your version up to date as the author updates their code.

Lets add the package from Einhugur

Right click on the top item in the left hand “navigator”

You should get a menu like the following

Select Manage Nuget Packages

You’ll see a dialog that has a very long list of packages that are available to be added to projects. There are packages for dealing with AWS, JSON, Azure, Unit Testing, Redis, REST API’s, and tons of other things.

Fortunately there’s a search field to help us narrow down the item list to the one we’re looking for.

In the search field type in Einhugur

Check the checkbox next to the package and press the Add Package button in the lower right

You’ll get asked what “project” to add the package to. Since we only have one project in this solution the dialog offers to add it to that one. Press the OK button and the package will be added.

ABOUT C# and VS

A “solution” can have many projects in it so it can make sharing code between the different project in the solution really simple, You can have a desktop app, a web app, a mobile app (iOS and Android) all in the same “solution”.

And now we’re ready to start altering the default code we were initially given when we created the solution.

In C# you have to tell the compiler what other bits of any project your going to use. You do this with a “Using” directive. In Xojo everything is global so you rarely have to do this. In C# using .Net the range of libraries is so vast its a requirement. And because there are often several different implementations for something like JSON you have to say which one you’re using – you could use several in different parts of your project (although you might regret that)

In VS, unlike in Xojo, each item in the left hand pane is a “file” a single file on disk. And VS is, for all intents and purposes a text editor for those files (albeit a very smart one).

When you double click an item in its left hand navigator it will open a tab with that item in the tab – the entire item.

What we want to start editing is the AppDelegate.cs file
Double click it to open it.

Since we’re going to make this application a Einhugur Forms one we’ll need to add the using clause
using Einhugur.Forms;

right below
using AppKit;
using Foundation;

As well since we’re not using macOS classes directly we’ll change the code for the class to

namespace getting_started
{
	[Register ("AppDelegate")]
	public class App : Application
        {
	   public App ()
		{
		}

           static void Main(string[] args)
           {
              Application.Init(typeof(App), args);
           }
    }
}

While all this looks daunting its not so hard in reality

namespace getting_started defines a namespace that all our project code can be in. Unlike Xojo “modules” a namespace in C# can span multiple files.

[Register (“AppDelegate”)] tells the C# compiler the following code implements the “AppDelegate” protocol. Its much like an interface.

public class App : Application declares the class App as a subclass of Application. In Xojo you’d set the super property.

public App () declares the CONSTRUCTOR for the App class. You can have several that vary by their signatures. Our code in this case doesnt need to do anything hence the empty { } that follows it.

static void Main(string[] args) declares the MAIN ENTRY POINT for the application. This is the FIRST code that will be run when this application starts up (even before the App constructor) This code basically calls the constructor of App and all that instance to initialize.

Thats it for code we need to write.

We do need to remove one file from the project since it servers the same purpose as the Main method we just added.

Right click on Main.cs and remove it from the project.

And now you can click “Run”

Heres my copy of the project as it sits now