Reputation: 101
My application have some UI issues in Mac Os Mojave.Some labels and buttons text content are not visible when I switched to Dark Mode.So I did one workaroud using following code.
var interfaceStyle = NSUserDefaults.StandardUserDefaults.StringForKey("AppleInterfaceStyle");
if (interfaceStyle == "Dark") {
label.textcolor = NSColor.White;
}
This fixes the issues,But if I switched back to light mode in between the application is using the label color will not change.I need to restart the application to read the code and displaying the label with default color.
Could Any one faced this issue ? Is there any delegate method that hits when the user changes the Appearance mode(Dark & Light) of Mac Os Mojave ?
Upvotes: 2
Views: 677
Reputation: 9990
At least for me it appears that the last line from SushiHangover's answer causes the crash on macOS Monterey and latest version of Xamarin. What works for me instead is:
NSDistributedNotificationCenter.GetDefaultCenter().RemoveObserver(this);
Upvotes: 0
Reputation: 74209
You can use KVO
to track theme changes (AppleInterfaceThemeChangedNotification
).
readonly NSString themeKeyString = new NSString("AppleInterfaceThemeChangedNotification");
readonly NSString dark = new NSString("Dark");
readonly Selector modeSelector = new Selector("themeChanged:");
[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");
}
}
NSDistributedNotificationCenter.GetDefaultCenter().AddObserver(this, modeSelector, themeKeyString, null);
Note: I typically register this in AppDelegate.DidFinishLaunching
NSDistributedNotificationCenter.GetDefaultCenter().RemoveObserver(this, themeKeyString);
BTW: The NSDistributedNotificationCenter.DefaultCenter.AddObserver
helpers/overloads do not work properly in this instance...
Upvotes: 4