I have three radio buttons for selecting my app's theme: Default
, which should apply whichever theme is selected in Android's system settings, Light
and Dark
.
The problem is that whenever I select the Default
radio button it doesn't return a standard value as I am expecting, but either OSAppTheme.Light
or OSAppTheme.Dark
, whichever the previous setting was. In other words it reapplies the previous setting.
Here is my code:
private void DarkMode(object sender, CheckedChangedEventArgs e)
{
if (defaultRadioButton.IsChecked == true)
{
if (Application.Current.RequestedTheme != OSAppTheme.Unspecified)
{
Application.Current.UserAppTheme = Application.Current.RequestedTheme;
}
else
{
Application.Current.UserAppTheme = OSAppTheme.Light;
}
}
else if (lightRadioButton.IsChecked == true)
{
Application.Current.UserAppTheme = OSAppTheme.Light;
}
else if (darkRadioButton.IsChecked == true)
{
Application.Current.UserAppTheme = OSAppTheme.Dark;
}
}
I had the impression that Application.Current.RequestedTheme
always carried the system's setting, which I guess from the behavior I'm encountering isn't true.
If Application.Current.RequestedTheme
doesn't get the system's theme setting, then which is the correct way to detect if a user has enabled Dark Mode
at the OS level?