Disable Dark Mode in Xamarin.Forms 4.8

Meisam Dehghan 46 Reputation points
2021-01-15T09:32:28.57+00:00

I've been developing a Xamarin.Forms app,and I'd like to disable the Dark mode.Currently,colors change as the Dark mode is enabled on the user's device.
To avoid that,I added the following line of code in the App.xaml.cs,but it didn't affect the automatic change of colors.Notice that I'm using a Shell type of application.
App.Current.UserAppTheme = OSAppTheme.Light;

Developer technologies | .NET | Xamarin
0 comments No comments
{count} vote

Accepted answer
  1. Lucas Zhang - MSFT 341 Reputation points
    2021-01-15T11:38:15.38+00:00

    Hello,

    Welcome to our Microsoft Q&A platform!

    in iOS

    If you want to disable the dark mode for your entire application you can add the key below in the info.plist:

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

    But it seems Apple requires us to implement the Dark Mode for iOS 13 and if you disable it using this key your app will be probably rejected when publishing. The doc says it is only used for temporarily:
    https://developer.apple.com/documentation/xcode/supporting_dark_mode_in_your_interface/choosing_a_specific_interface_style_for_your_ios_app?language=objc
    You can try to create a custom renderer for your ContentPage and disable it there like:

    [assembly: ExportRenderer(typeof(ContentPage), typeof(CustomPageRenderer))]
    namespace Demo.iOS
    {
        public class CustomPageRenderer : PageRenderer
        {
            protected override void OnElementChanged(VisualElementChangedEventArgs e)
            {
                base.OnElementChanged(e);
    
                OverrideUserInterfaceStyle = UIUserInterfaceStyle.Light;
            }
        }
    }
    

    If you are consuming a NavigationPage do the same setting for it:

    [assembly: ExportRenderer(typeof(NavigationPage), typeof(CustomNaviPageRenderer))]
    namespace Demo.iOS
    {
        public class CustomNaviPageRenderer : NavigationRenderer
        {
            protected override void OnElementChanged(VisualElementChangedEventArgs e)
            {
                base.OnElementChanged(e);
    
                OverrideUserInterfaceStyle = UIUserInterfaceStyle.Light;
            }
        }
    }
    

    in Android

    Add the line in MainActivity ->OnCreate

    AppCompatDelegate.DefaultNightMode = AppCompatDelegate.ModeNightNo;
    

    Best Regards,

    Lucas Zhang

    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    4 people found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. José Pedro Leao Pereira 11 Reputation points
    2021-05-14T08:46:24.917+00:00

    In Android this did the trick for me:

    <style name="MainTheme" parent="Theme.AppCompat.Light.NoActionBar">

    2 people found this answer helpful.
    0 comments No comments

  2. Bhuvanesh Gunasekaran 1 Reputation point
    2021-11-18T10:55:49.977+00:00
    AppCompatDelegate.DefaultNightMode = AppCompatDelegate.ModeNightNo;
    

    This is working for me

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.