Xamarin.Mac/スリープからの復帰を検出する
2016年5月18日 (水) 02:29時点におけるimported>Administratorによる版 (→関連項目)
目次
概要
実装
スリープ状態はNotificationCenterから取得することができる。
「コンピューターのスリープ」と「ディスプレイのスリープ」が別通知なので注意すること。この例では「コンピューターのスリープ」のみ対応しているが、現実的は両方を記述することになると思う。
コンピューターのスリープ突入 | NSWorkspace.WillSleepNotification |
コンピューターのスリープ復帰 | NSWorkspace.DidWakeNotification |
ディスプレイのスリープ突入 | NSWorkspace.ScreensDidSleepNotification |
ディスプレイのスリープ復帰 | NSWorkspace.ScreensDidWakeNotification |
using System;
using AppKit;
using Foundation;
namespace NotificationSleepWake
{
[Register("AppDelegate")]
public class AppDelegate : NSApplicationDelegate
{
public AppDelegate()
{
}
NSObject _wake;
public override void DidFinishLaunching(NSNotification notification)
{
// Insert code here to initialize your application
if (_wake == null)
{
_wake = NSWorkspace.SharedWorkspace.NotificationCenter.AddObserver(
NSWorkspace.DidWakeNotification,
(notify) =>
{
Console.WriteLine("wake");
});
}
}
public override void WillTerminate(NSNotification notification)
{
if (_wake != null)
{
NSWorkspace.SharedWorkspace.NotificationCenter.RemoveObserver(_wake);
}
}
}
}