With photos: http://www.netneurotic.net/Mono/MonoMac-windowWillClose.html
The trick here is to create a method that calls Environment.Exit () to exit the application, like any other .NET application.
Another trick is that Environment.Exit () does not work when Cocoa objects are alive. But NSApplication.SharedApplication.Terminate (this) works. I do not know how to return the error code in this way.
Cocoa, and thus MonoMac uses βdelegatesβ to allow one object to react when something happens to another object. We will use this concept to call Terminate () when the main window closes.
A Cocoa object has βexitsβ that appear to point to other objects. I do not know if this is a technically correct description. It does not matter. One way out is the "delegate", which we will set for the object that contains the method that we want to call when the window is closed. Therefore, we must set the delegate output of the main window to our object.
We will use the MainWindow class defined in MainWindow.cs as a delegate for the main window. I suppose this means that we use the object as its own delegate or something like that. It will work anyway.
To make MainWindow a delegate for the main window and respond to closing the main window, follow these steps:
Double-click MainWindow.xib to open Xcode.
In Xcode, find the main window. This is a big thing that looks like a window.
Figure 1: The main window called "Window" is the main window.
- Right-click on the window title bar to display output windows.
You will see one exit called "delegate".
Figure 2: One of the "Outlets" is the "delegate".
- Find the "Object Library" and the blue box. The blue square is an object.
Figure 3: The blue square is an object.
- Drag the blue square to the gray tree using the icons to the left of the window.
Figure 4: The blue square belongs below the window icon.
- Make the blue box an object "MainWindow". Click on the blue frame and change its class to "MainWindow".
Figure 5: Update the class name for the blue field.
- Press the control key and drag it from the title bar of the window to the blue square. Then select the "delegate" option in the menu that appears.
Figure 6: The menu that appears when you control the drag and drop.
Our MainWindow object is now a delegate for the main window. This means that he can respond to events that occur with the window.
Add the following code to MainWindow.cs:
[Export ("windowWillClose:")] public void WindowWillClose (NSNotification notification) {Console.WriteLine ("windowWillClose:"); NSApplication.SharedApplication.Terminate (this);
}
The operator [Export ("WindowWillClose:")] tells the compiler (presumably the compiler, but maybe some other utility technically says this) that the declaration of the next method is the C # equivalent of the declared Objective-C method. The method may have a different actual name, but it should be called similar enough so that we can easily identify it. Usually I just change the first letter to its uppercase version to fit C # style.
- Compile the application, correct any typos you made, and then run it. Try closing the window. The application will be closed.
If not, repeat all the steps listed here until this happens.