You can use KVO
to track theme changes (AppleInterfaceThemeChangedNotification
).
A few class level "constants":
readonly NSString themeKeyString = new NSString("AppleInterfaceThemeChangedNotification");
readonly NSString dark = new NSString("Dark");
readonly Selector modeSelector = new Selector("themeChanged:");
Export method for the ObjC selector to call:
[Export("themeChanged:")]
public void ThemeChanged(NSObject change)
{
var interfaceStyle = NSUserDefaults.StandardUserDefaults.StringForKey("AppleInterfaceStyle");
if (interfaceStyle == "Dark")
{
Console.WriteLine("Now Dark");
}
else
{
Console.WriteLine("Now not Dark");
}
}
Add observer request to notification center:
NSDistributedNotificationCenter.GetDefaultCenter().AddObserver(this, modeSelector, themeKeyString, null);
Note: I typically register this in AppDelegate.DidFinishLaunching
Remove the observer if you no longer need it:
NSDistributedNotificationCenter.GetDefaultCenter().RemoveObserver(this, themeKeyString);
BTW: The NSDistributedNotificationCenter.DefaultCenter.AddObserver
helpers/overloads do not work properly in this instance...