3
votes

I am developing a Cocoa application with MonoMac (C#).

I have a class MyWindowController that inherits MonoMac.AppKit.NSWindowController, and open a new instance of this window like this:

MyWindowController mwc = new MyWindowController();
mwc.Window.MakeKeyAndOrderFront(this);

But how do I open it as a modal dialog? It is imperative that nothing else in my application is executed while the dialog is open, so I cannot use a window sheet (which only blocks the current window). And I can't find anything else that seems to do what I want on my controller. On Windows, I would have done this simply by calling:

mwc.ShowDialog();

So what I want is the MonoMac equivalent of ShowDialog(), I believe.

2

2 Answers

4
votes

I spent almost three hours trying to figure this out before posting the question, but of course I found the solution right after asking.

It looks like I need to use the NSApplication object:

NSApplication.SharedApplication.RunModalForWindow(ewc.Window);
2
votes

I had success with the answer above, but then had trouble dismissing the modal in the case that the 'close' button was pressed. The solution was to add the code

[Export ("windowWillClose:")]
    public void WindowWillClose(NSNotification notification)
    {
        Console.WriteLine("windowWillClose:");
        NSApplication.SharedApplication.StopModal ();
    }

to the window controller, then set the window controller to be the window's delegate by, in Interface Builder, right-click the window and drag a line from "delegate" to the "file's owner" block.