Xamarin.MacでStoryboard上のWindowをモーダルで出す


忘備録として

単なる忘備録です。
今更感満載 です。

Xanarin.Macでウインドウをモーダルで出す

出すにしても、Storyboard上にある、NSWindowControllerに結びついているウインドウを出したりします。
主に使いそうなのは、環境設定... 用です。

StoryboardからNSWindowController(idつき)を引っ張り出す

InstantiateControllerWithIdentifier を使います。

var wc = NSStoryboard.MainStoryboard.InstantiateControllerWithIdentifier("preference window") as NSWindowController;

モーダルで出す

RunModalForWindow を使います。

NSApplication.SharedApplication.RunModalForWindow(wc.Window);

モーダルを閉じる

StopModalOrderOut を使います。

NSApplication.SharedApplication.StopModal();
wc.Window.OrderOut(wc);

一連の流れを関数にする

メニューに関連づけます。
こんな感じになります。

partial void doPreferences(Foundation.NSObject sender)
{
    var wc = NSStoryboard.MainStoryboard.InstantiateControllerWithIdentifier("preference window") as NSWindowController;
    if (wc != null)
    {
        var closeButton = wc.Window.StandardWindowButton(NSWindowButton.CloseButton);
        closeButton.Activated += (object clickSender, System.EventArgs e) =>
        {
            NSApplication.SharedApplication.StopModal();
            (closeButton.AccessibilityParent as NSWindow).OrderOut(closeButton);
        };
        NSApplication.SharedApplication.RunModalForWindow(wc.Window);
    }
}

なんで書いておくのか

Windowのクローズボタンからの処理に悩んだからです…。
なので、一番書きたかったのは、 StandardWindowButton の部分だったりします。