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?
According to this case about Manually apply dark theme in Xamarin.Forms, the
Application.Current.UserAppThemeonly applys for the theme defined with AppThemeBinding.So if you want to change the UI's theme with this property, you need to use the AppThemeBinding in your control such as the example code in the official document.
In addition, the
Application.Current.RequestedThemecan really get the device's system's current theme. But you can also try to get the android device's current theme with the following code in the MainActivity:And then you can declare a dark theme in the Project.Droid/Resource/values/style.xml and use it or just use the
AppThemeBindingas the official document does to apply the dark theme in your application.Update
The value will be dark. It should be a bug, you can report it on the github.