In order to take advantages of the Monobjc bridge, there is some standard things to do in the executing assembly entry point. In Cocoa, the main function calls the NSApplicationMain function that performs a lot of things behind the curtain. With the Monobjc bridge, the starting sequence is merely the same for every applications.

Console Application

A console application does not require a graphical interface, which means that no NIB is needed. The bootstrapping typically looks like:

Here is the main steps of the code:

using System;
using Monobjc; // (1)
using Monobjc.Foundation;

namespace Monobjc.Samples.MyApplication
{
    /// 
    /// Entry point for the application
    /// 
    internal class Program
    {
        public static void Main(String[] args) // (2)
        {
            // Load any required framework
            ObjectiveCRuntime.LoadFramework("Cocoa"); // (3)
            // Do the bridge initialization
            ObjectiveCRuntime.Initialize(); // (4)

            NSAutoreleasePool pool = new NSAutoreleasePool(); // (5)

            // ... (6)

            pool.Release(); // (7)
        }
    }
}
  1. Import the Monobjc namespaces. These two namespaces are often used as they represents both the Monobjc core library and the Monobjc Foundation API wrapper.
  2. This is the standard way to define an application entry-point
  3. This line is mandatory for the Monobjc bridge to run properly. Native libraries must be loaded before the wrapper because the Monobjc core libraries need them when it creates the dynamic proxies of the Objective-C classes and categories.
  4. This line initializes the bridge (registering wrappers, creating proxies, etc).
  5. An NSAutoreleasePool is recommended to avoid memory leaks. The creation is explicit.
  6. The actual code lies here.
  7. Before exiting the application, we release the NSAutoreleasePool in order to cleanup the memory.

Cocoa Application

A console application requires one or more graphical interface, which means that one or more NIB will be loaded. The bootstrapping typically looks like:

Here is the main steps of the code:

using System;
using Monobjc; // (1)
using Monobjc.AppKit;
using Monobjc.Foundation;

namespace Monobjc.Samples.MyApplication
{
    /// 
    /// Entry point for the application
    /// 
    internal class Program
    {
        public static void Main(String[] args) // (2)
        {
            // Load any required framework
            ObjectiveCRuntime.LoadFramework("Foundation"); // (3)
            ObjectiveCRuntime.LoadFramework("AppKit");
            // Do the bridge initialization
            ObjectiveCRuntime.Initialize(); // (4)

            NSApplication.Bootstrap(); // (5)
            NSApplication.LoadNib("MainMenu.nib"); // (6)
            NSApplication.RunApplication(); // (7)
        }
    }
}
  1. Import the Monobjc namespaces. These three namespaces are often used as they represents both the Monobjc core library, the Monobjc Foundation API wrapper and the Monobjc AppKit API wrapper.
  2. This is the standard way to define an application entry-point
  3. This line is mandatory for the Monobjc bridge to run properly. Native libraries must be loaded before the wrapper because the Monobjc core libraries need them when it creates the dynamic proxies of the Objective-C classes and categories.
  4. This line initializes the bridge (registering wrappers, creating proxies, etc).
  5. The bootstrapping of a GUI application involves the creation of a NSAutoreleasePool instance.
  6. The NIB file is loaded and the NSApplication instance is the owner.
  7. The method launch the event loop processing of the NSApplication instance.

Note

Don’t put final cleanup code in your application’s Main method after the RunApplication call: it will never be executed. If some cleanup is necessary, set up a delegate that responds to the applicationWillTerminate message and performs in that method.