Opening a Cocoa window in MonoMac

2019-07-29 07:47发布

问题:

I am trying to accomplish the very simple task of opening a new window, but I seem to be completely unable to do so.

I am using MonoMac with monobjc on a Mac. When creating a new monobjc project, a MainMenu.xib is created. I have managed to add controls and binding to this file using the XCode editor. This window is automatically opened when the application is launched.

When I attempt to create a new window, however, I can't make it show up at all. I created a .xib file by right clicking the parent folder of MainMenu.xib, and clicking Add file => Cocoa Window Template. This file can be edited in XCode just a MainMenu.xib.

I then proceeded to create a class inheriting NSWindowController, and I chose this class as the "File's owner" of the window in my .xib file. I have then been able to design the window, and to add outlets and actions to the controller.

But I can't figure out how to open the window. I have tried multiple variations of this:

var f = new RegularLoginForm(); //which inherits NSWindowController
f.LoadWindow(); //I have tried without this line
f.ShowWindow(null); // I have also tried sending in the AppDelegate object (which is the controller for MainMenu.xib)

But nothing seems to work -- that is, the window never opens. Any suggestions?

EDIT: Here's a screenshot of how my .xib and it's configuration looks: https://dl.dropbox.com/u/1545094/so_nswindowcontroller.png

回答1:

You set RegularLoginForm as File's Owner in your Window.xib; but did you also connect the Window outlet from File's Owner to the Window in Interface Builder?

i.e. Right-click on File's Owner in IB, and click-drag the Window outlet to the Window object

Further to that, I tried your code and for me it crashes on the call to

f.ShowWindow(null)

[ERROR] FATAL UNHANDLED EXCEPTION: System.ArgumentNullException: Argument cannot be null. Parameter name: sender

It runs fine if I assign the sender parameter, e.g.

f.ShowWindow(this)

However, my environment is only MonoMac; no monoobjc (I'm not sure how or why you are using both together?); so YMMV.