The first thing to do is create a NIB file for use with the application. A NIB file is so much easier to use than creating the entire window and controls via code.
Actions: Actions are like events. Creating an action in a nib will allow you to assign it to any control that has event capabilities. Note: You can also assign actions via code (ie "button1.action = buttonClick")
Actions are connected by Ctrl Clicking/Dragging from the Interface Element to the Controller Object.
Outlets: Outlets are a way to tie objects in the GUI to variables in code. If you don't have the outlet tied to anything then you can't use it. It's a way to tell the Code what variable is pointing to what interface element.
Outlets are connected by Ctrl Clicking/Dragging from the Controller Object to the Interface Element.
The main.cs file is the entry point to the app. We split this off from the actual ""ApplicationController"" so that we can keep track of things easily.
using System; using System.Runtime.InteropServices; using Cocoa; class MainClass { public void Run() { Application.Init(); Application.LoadNib ("Main.nib"); Application.Run(); } static void Main(string[] args) { MainClass main = new MainClass(); main.Run(); } }
The important thing to consider here is the "Application.LoadNib()" line. Make sure it's loading the proper NIB file.
The applicationcontroller.cs is the main interface handling point. This is where all the GUI elements are connected and defined.
using System; using System.IO; using System.Collections; using Cocoa; [Register("ApplicationController")] public class ApplicationController : Cocoa.Object { [Connect] public TextField textField1; [Connect] public Window mainWindow; protected ApplicationController(System.IntPtr a) : base(a){} //Form.Load Event [Export("applicationWillFinishLaunching:")] public void FinishLoading(Notification aNotification) { textField1.Value = "Form Loaded"; } [Export("buttonClick:")] public void buttonClick(object sender) { textField1.Value = "Button Pushed"; } }
I'm not sure what the technical explanation for this is, but this is the part that connects the variable you define to the object from the nib. You can have interface elements that do not have a local variable in the code, but you cannot connect local variables to non existent interface elements. Doing so will cause the app to crash on launch. For every interface element that you want to be able to access via code, you will need to create a new variable of the proper type and make sure that you use the "Connect" statement above it.
The Register statement above the beginning of the class connects this class to the "ApplicationController" from the NIB.
The export tag is the one that ties local events to the events declared in the NIB. You can assign these events to an interface object a couple of ways. You can Ctrl Click and drag from the interface element to the controller class, or you can define it via code. The NIB method is normally easiest but some interface elements don't like it.
In the above example you can see that we have exported "buttonClick:" to my local buttonClick handler. The "applicationWillFinishLaunching:" event is generated by the (app/main window). Think of it like the FormLoad event in winforms (as far as I can tell).
You could just type the necessary commands into a Terminal window, but creating a Makefile means you can quickly recompile if you make a mistake or you want to go in and tweak the application.
Create a file called "Makefile" with the following code.
all: test test: Main.cs mcs -t:exe -out:SimpleTutorial.exe -pkg:cocoa-sharp *.cs rm -rf SimpleTutorial.app macpack -m:2 -n:SimpleTutorial -o:. -a:SimpleTutorial.exe -r:Main.nib
Then open a Terminal window, change to the directory where you have saved your files and type the command "make". This will compile your CocoaSharp code with the Mono compiler, and then create a Mac OS X application from the compiled code and the nib file that defines the UI.
The -pkg option on the mcs command makes sure that it's compiled with the cocoasharp package. MacPack is the tool that creates the app bundles for OS X. Make sure you include any nibs/images/icons/etc as a resource by using the -r option of macpack.
Well, you should now be finished. If all went well you should see an application called SimpleTutorial. Double click on it, and you should see the SimpleTutorial application running.
Click on the button and you should see the text "Form Loaded" change to "Button Pushed".