Come up with this so far:
public void appearanceAuto(NSObject sender)
{
((NSMenuItem)sender).State = NSCellStateValue.On;
Application.Current.UserAppTheme = OSAppTheme.Unspecified;
Preferences.Set("AppTheme", (int)OSAppTheme.Unspecified, "Global");
window.Appearance = null;
}
[Action("appearanceLight:")]
public void appearanceLight(NSObject sender)
{
((NSMenuItem)sender).State = NSCellStateValue.On;
Application.Current.UserAppTheme = OSAppTheme.Light;
window.Appearance = NSAppearance.GetAppearance(NSAppearance.NameAqua);
Preferences.Set("AppTheme", (int)OSAppTheme.Light, "Global");
}
[Action("appearanceDark:")]
public void appearanceDark(NSObject sender)
{
((NSMenuItem)sender).State = NSCellStateValue.On;
Application.Current.UserAppTheme = OSAppTheme.Dark;
window.Appearance = NSAppearance.GetAppearance(NSAppearance.NameDarkAqua);
Preferences.Set("AppTheme", (int)OSAppTheme.Dark, "Global");
}
It sets the check item correctly but I don't know get to get at the other two menu items to turn theirs off?
I managed to consolidate the actions into one like this:
[Action("appearanceChanged:")]
public void appearanceChanged(NSObject sender)
{
var menuItem = (NSMenuItem)sender;
if (menuItem == null)
return;
NSAppearance appearance = null;
OSAppTheme theme = OSAppTheme.Unspecified;
var identifier = menuItem.Identifier;
if (identifier == "appearanceDark")
{
appearance = NSAppearance.GetAppearance(NSAppearance.NameDarkAqua);
theme = OSAppTheme.Dark;
}
else if (identifier == "appearanceLight")
{
appearance = NSAppearance.GetAppearance(NSAppearance.NameAqua);
theme = OSAppTheme.Light;
}
window.Appearance = appearance;
Application.Current.UserAppTheme = theme;
Preferences.Set("AppTheme", (int)theme, "Global");
menuItem.State = NSCellStateValue.On;
}
}
This article helped me.
This answer to a similar question on StackOverflow shows how to do it by having a IBOutlet
and using that to loop the menu items. But I am not doing this in Xcode. I am doing this in Visual Studio for Mac and I don't know how to do that bit.
I see this tutorial explain about using IBAction
but it refers to connection into ViewController.h
and my MacOS project does not have such a file.
For some reason, when I try to CONTROL + Drag it only wants to make a IBAction and not a IBOutlet:
I have now found the answer to this issue. Firstly, I did not have to actually set the State
inside my action handler. It could be done inside the validateMenuItem
event handler instead (which I was lacking).
I had to open my menu in Xcode first and assign the Tag property for each menu item. Then I was able to do the following:
[Export("validateMenuItem:")]
public bool validateMenuItem(NSMenuItem menuItem)
{
switch(menuItem.Tag)
{
case 1000:
if (Application.Current.UserAppTheme == OSAppTheme.Unspecified)
menuItem.State = NSCellStateValue.On;
else
menuItem.State = NSCellStateValue.Off;
break;
case 1001:
if (Application.Current.UserAppTheme == OSAppTheme.Light)
menuItem.State = NSCellStateValue.On;
else
menuItem.State = NSCellStateValue.Off;
break;
case 1002:
if (Application.Current.UserAppTheme == OSAppTheme.Dark)
menuItem.State = NSCellStateValue.On;
else
menuItem.State = NSCellStateValue.Off;
break;
}
return true;
}
Works well!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.