LoginSignup
1
2

More than 5 years have passed since last update.

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

Posted at

忘備録として

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

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 の部分だったりします。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2