Pergunta

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.

Foi útil?

Solução

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);

Outras dicas

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top