Xamarin.Macでディスプレイの設定変更を検出する

提供: MonoBook
ナビゲーションに移動 検索に移動

Xamarin.Macディスプレイ周りの設定がなにかしら変更された際にそれを検出できると色々嬉しい。

概要

Xamarin.Macディスプレイ周りの設定がなにかしら変更された際にそれを検出できると色々嬉しい。 そんなことより世の中にはなんと外部ディスプレイを接続されるだけでクラッシュするクソアプリがあるらしい。デバッグはちゃんとしよう。

実装1

AppDelegateのScreenParametersChangedメソッドをオーバーライドするだけで検出できる。

    public partial class AppDelegate : NSApplicationDelegate
    {
        MainWindowController mainWindowController;

        public AppDelegate()
        {
        }

        public override void DidFinishLaunching(NSNotification notification)
        {
            mainWindowController = new MainWindowController();
            mainWindowController.Window.MakeKeyAndOrderFront(this);

            ScreenInfo();
        }

        public override void ScreenParametersChanged(NSNotification notification)
        {
            ScreenInfo();
        }

        void ScreenInfo()
        {
            foreach (var screen in NSScreen.Screens)
            {
                Console.WriteLine(screen.Frame);
                Console.WriteLine(screen.BackingScaleFactor);
                Console.WriteLine(screen.Frame.Width * screen.BackingScaleFactor + "," + screen.Frame.Height * screen.BackingScaleFactor);
                Console.WriteLine(screen.DeviceDescription);
                Console.WriteLine("----");
            }
        }
    }

関連項目

参考文献