Interesting Tech Projects
Global Objects With MonoMac
When developing for C# and using Windows Forms creating and using global objects is trivial:
public static class Program { public static Foo MyFoo = new Foo(); static void main() { Main = new MainForm(MyFoo); } }
The same can’t be said for MonoMac. The mistake is to equate the static MainClass class as being the same as the WinForms Program class in the example shown.
The solution is to put the global class instantiation inside the NSApplication delegate. MonoMac creates this automatically and calls it AppDelegate. The above example becomes:
public partial class AppDelegate : NSApplicationDelegate { MainWindowController mainWindowController; public Foo MyFoo = new Foo(); public AppDelegate () { } public override void FinishedLaunching (NSObject notification) { mainWindowController = new MainWindowController(MyFoo); mainWindowController.Window.MakeKeyAndOrderFront(this); } }
Print article | This entry was posted by Andy on September 24, 2011 at 9:12 pm, and is filed under Mac OS X, Software Engineering. Follow any responses to this post through RSS 2.0. Both comments and pings are currently closed. |
Comments are closed.