/// <summary>Helper used to create an NSApplication subclass.</summary> /// <param name = "appClass">The name of the subclass, e.g. "MyApplication".</param> /// <param name = "nibName">The name of a nib file, e.g. "MainMenu.nib".</param> /// <param name = "extendDebugMenu">Used to add custom items to the debug menu. May be null.</param> public static NSApplication Create(string appClass, string nibName, Action<NSMenu> extendDebugMenu) { NSApplication app = new Class(appClass).Call("sharedApplication").To<NSApplication>(); ms_mainThread = Thread.CurrentThread; // Load our nib. This will instantiate all of the native objects and wire them together. // The C# objects will be created the first time a managed method is called. NSMutableDictionary dict = NSMutableDictionary.Create(); bool loaded = NSBundle.mainBundle().loadNibFile_externalNameTable_withZone( NSString.Create(nibName), dict, IntPtr.Zero); if (!loaded) throw new InvalidOperationException("Couldn't load " + nibName + "."); // We need an NSAutoreleasePool to do Native.Call, but we don't want to have one // hanging around while we're in the main event loop because that may hide bugs. // So, we'll instantiate a Native instance here and call Invoke later which can // be done without an NSAutoreleasePool. ms_run = new Native(app, new Selector("run")); ms_helper = AppHelper.Create(); #if DEBUG app.BeginInvoke(() => DoInitDebugMenu(extendDebugMenu)); #endif ms_startupPool.release(); ms_startupPool = null; return app; }
internal static object Call(IntPtr instance, string name, object[] args) { Contract.Requires(instance != IntPtr.Zero, "instance is zero"); object result; if (name == "alloc" && args.Length == 0) // need this so we can create an auto release pool without leaking NSMethodSignature { IntPtr exception = IntPtr.Zero; IntPtr ip = DirectCalls.Callp(instance, Selector.Alloc, ref exception); if (exception != IntPtr.Zero) CocoaException.Raise(exception); result = NSObject.Lookup(ip); } else { Selector selector = new Selector(name); MethodSignature sig = new MethodSignature(instance, (IntPtr) selector); Native native = new Native(instance, selector, sig); native.SetArgs(args); result = native.Invoke(); } return result; }