Creating our Window in C#

So far when you run the project it will show a main window.
But this is because we created the kind of project we did with a main storyboard and an entry in the Info.plist that told macOS what should be the main window.

It isn’t one we created in our new framework.

Lets rectify that.

First open Info.plist and remove the entry for NSMainStoryBoardFile. Click on the small red X icon at the right end of the entry name.

As well remove the Main.storyboard item from the project. Right click on it and select Delete.

At this point IF you run the app wont open any windows, but should still run (Check the main menu bar to verify)

Now to create our “default window” we’ll add a class to our project

Name this new class MainWindow

Notice that the IDE has no idea what superclass this new class should inherit from. Lets fix that.

Recall that in C# we MUST tell the compiler what items we are going to be “using”. Since the Einhugur framework includes a definition for a Window we can tell our new class to inherit from that by adding a using clause
using Einhugur.Forms;
following the using System; that already exists and altering the class declaration to
public class MainWindow : Window

This style says “the class MainWindow inherits from Window”
And the using clause helps the compiler find the declaration of Window in the Einhugur.Forms package

The other thing we can do is remove the Constructor – public MainWindow() since we dont need to do anything special in it

But how will this window get “set up” ? Note there is NOT visual designer in which we can set properties like size, placement, title etc.

We’ll add a method to our new window that takes care of all this. In Xojo this also happens just the IDE writes the code based on your visual design.

In this case its a method that the base Window defines that does nothing that we can override with our own so we can set things how we want.
Add a method to the class by just typing in between the { and } that follow the class declaration

    // much like the constructor in Xojo
    protected override void SetupWindow(WindowCreationParameters prm)
    {
        prm.Left = 200;
        prm.Top = 100;
        prm.Width = 600;
        prm.Height = 400;
        prm.Title = "My first window";
        prm.InitialPlacement = WindowPlacement.Center;
    }

One other thing is the application doesnt know that it should show this window when it starts.

Switch to the AppDelegate and we’ll add an override of the Opened method. This one is much like Xojo’s Opening event when the app starts. This gets called automatically as part of the application startup

    protected override void Opened()
    {
        var window = new MainWindow();

        window.Show();
    }

When you run now you should get one window opened.
Note that there is still some work to do as we haven’t attached any menu bar. Without a menu bar the QUIT menu item doesn’t appear or function so you have to KILL the running debug app by pressing the square STOP icon

Here’s my app so far