Search code examples
c#xamarinmaui

How to switch to light mode despite dark mode setting for the phone


In my application I have defined colors for my controls. Mostly like this

{StaticResource Black}

some like this because I had some problems with changing the color when I set the value to disabled

{AppThemeBinding Light={StaticResource Gray200}, Dark={StaticResource Gray200}}

Addition, when starting the application, I set the mode to Light in this way:

Current.UserAppTheme = AppTheme.Light;

But even so, if the phone has any settings for dark mode, my dialogues triggered in this way

Application.Current.MainPage.DisplayAlert

have a gray background instead of white. And there are some cases where, for example, the Editor has a black background instead of a white one.

How do I force my phone to go into light mode when using my app?


Solution

  • To prevent Dark mode in Maui you need to force it by applying it natively.

    In my opinion, this is the better solution than applying hacks in cross-platform code, Even though not handling dark mode is a hack in itself.

    In your native android activity in the onCreate method add the following:

    AppCompatDelegate.DefaultNightMode = AppCompatDelegate.ModeNightNo;
    

    Whereas for iOS you need to edit the Info.plist file and add the following:

    <key>UIUserInterfaceStyle</key>
    <string>Light</string>
    

    Some devices might need you to add the following to your App's constructor:

    public App()
    {
       Application.Current.UserAppTheme = AppTheme.Light;
    }
    

    This is probably because MAUI is messing around with your themes in the background not sure how and why