{"id":820,"date":"2023-02-03T18:22:24","date_gmt":"2023-02-03T18:22:24","guid":{"rendered":"https:\/\/learnmobiledevelopment.com\/?p=820"},"modified":"2023-02-03T18:43:08","modified_gmt":"2023-02-03T18:43:08","slug":"prevent-dark-mode-in-xamarin-forms","status":"publish","type":"post","link":"https:\/\/learnmobiledevelopment.com\/index.php\/2023\/02\/03\/prevent-dark-mode-in-xamarin-forms\/","title":{"rendered":"How to Prevent Dark Mode in Xamarin forms"},"content":{"rendered":"\n<p>When an app runs on <strong>iOS 13<\/strong> and when a user flips to <strong>dark mode<\/strong> from settings then apps do not look so great. So there are two ways to handle this: either create different themes for <strong>Dark Mode<\/strong> and <strong>Light Mode<\/strong> and switch based on the system&#8217;s theme at runtime or prevent the <strong>Dark Mode<\/strong>. So In this article, I\u2019m going to show you how to Prevent Dark Mode in Xamarin Forms.<\/p>\n\n\n\n<p><strong>Dark Mode<\/strong> is introduced in <strong>iOS 13<\/strong> and In Android It&#8217;s introduced in <strong>Android 10<\/strong> (API Level 29) in android it is called <strong>Dark Theme<\/strong>.&nbsp;<\/p>\n\n\n\n<p style=\"font-size:25px\"><strong>Let&#8217;s Start<\/strong><\/p>\n\n\n\n<p>To force the light theme in application we need to do some changes in native side.<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>In iOS Side :<\/strong><\/p>\n\n\n\n<p>Add the following key to <strong>info.plist<\/strong> file :<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"xml\" data-enlighter-theme=\"dracula\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;key>UIUserInterfaceStyle&lt;\/key>\n&lt;string>Light&lt;\/string><\/pre>\n\n\n\n<p>We can also do this by creating custom renderer for either <strong>ContentPage<\/strong> or <strong>NavigationPage<\/strong> as follows :<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Using ContentPage Renderer :<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"dracula\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">[assembly: ExportRenderer(typeof(ContentPage), typeof(CustomPageRenderer))]\nnamespace Example.iOS\n{\n    public class CustomPageRenderer : PageRenderer\n    {\n        protected override void OnElementChanged(VisualElementChangedEventArgs e)\n        {\n            base.OnElementChanged(e);\n\n            OverrideUserInterfaceStyle = UIUserInterfaceStyle.Light;\n        }\n    }\n}<\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Using NavigationPage Renderer :<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"dracula\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">[assembly: ExportRenderer(typeof(NavigationPage), typeof(CustomNaviPageRenderer))]\nnamespace Example.iOS\n{\n    public class CustomNaviPageRenderer : NavigationRenderer\n    {\n        protected override void OnElementChanged(VisualElementChangedEventArgs e)\n        {\n            base.OnElementChanged(e);\n\n            OverrideUserInterfaceStyle = UIUserInterfaceStyle.Light;\n        }\n    }\n}<\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>In Android Side :<\/strong><\/p>\n\n\n\n<p>We have to put the below code to <strong>OnCreate()<\/strong> method in <strong>MainActivity<\/strong> :<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"dracula\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">AppCompatDelegate.DefaultNightMode = AppCompatDelegate.ModeNightNo;<\/pre>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>That\u2019s all for now!<\/p>\n\n\n\n<p>Happy Coding!<strong> \ud83d\ude00<\/strong><\/p>\n\n\n\n<div style=\"height:34px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-medium-font-size\"><strong>You may also like<\/strong><\/p>\n\n\n\n<div class=\"wp-block-group is-content-justification-space-between is-nowrap is-layout-flex wp-container-core-group-is-layout-0dfbf163 wp-block-group-is-layout-flex\">\n<div class=\"wp-block-group is-vertical is-content-justification-center is-layout-flex wp-container-core-group-is-layout-4b2eccd6 wp-block-group-is-layout-flex\"><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><a href=\"https:\/\/learnmobiledevelopment.com\/index.php\/2023\/01\/04\/get-device-and-app-informations-in-maui\/\" target=\"_blank\" rel=\"noreferrer noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2023\/01\/Device-And-App-Info-1024x684.png\" alt=\"get device and app informations in maui\" class=\"wp-image-653\" width=\"512\" height=\"342\" srcset=\"https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2023\/01\/Device-And-App-Info-1024x684.png 1024w, https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2023\/01\/Device-And-App-Info-300x200.png 300w, https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2023\/01\/Device-And-App-Info-768x513.png 768w, https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2023\/01\/Device-And-App-Info-850x568.png 850w, https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2023\/01\/Device-And-App-Info.png 1296w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p class=\"has-text-align-center\">How to get Device and App Informations in MAUI<\/p>\n<\/div>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-center is-layout-flex wp-container-core-group-is-layout-4b2eccd6 wp-block-group-is-layout-flex\">\n<div class=\"wp-block-group is-vertical is-content-justification-center is-layout-flex wp-container-core-group-is-layout-4b2eccd6 wp-block-group-is-layout-flex\"><div class=\"wp-block-image is-style-default\">\n<figure class=\"aligncenter size-large is-resized\"><a href=\"https:\/\/learnmobiledevelopment.com\/index.php\/2022\/12\/21\/uniformitemslayout-in-maui\/\" target=\"_blank\" rel=\"noreferrer noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2022\/12\/MAUIUniformItemsLayout-1024x548.png\" alt=\"UniformItemsLayout in MAUI\" class=\"wp-image-522\" width=\"512\" height=\"274\" srcset=\"https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2022\/12\/MAUIUniformItemsLayout-1024x548.png 1024w, https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2022\/12\/MAUIUniformItemsLayout-300x161.png 300w, https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2022\/12\/MAUIUniformItemsLayout-768x411.png 768w, https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2022\/12\/MAUIUniformItemsLayout-850x455.png 850w, https:\/\/learnmobiledevelopment.com\/wp-content\/uploads\/2022\/12\/MAUIUniformItemsLayout.png 1390w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p class=\"has-text-align-center\">How to use UniformItemsLayout in MAUI<\/p>\n<\/div>\n<\/div>\n<\/div>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When an app runs on iOS 13 and when a user flips to dark mode from settings then apps do not look so great. So there are two ways to handle this: either create different themes for Dark Mode and Light Mode and switch based on the system&#8217;s theme at runtime or prevent the Dark&#8230;<\/p>\n","protected":false},"author":1,"featured_media":831,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1],"tags":[73,74,76,72],"class_list":["post-820","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-xamarin","tag-dark-mode","tag-light-mode","tag-light-theme","tag-prevent-dark-mode-in-xamarin-forms"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/learnmobiledevelopment.com\/index.php\/wp-json\/wp\/v2\/posts\/820","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnmobiledevelopment.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnmobiledevelopment.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnmobiledevelopment.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnmobiledevelopment.com\/index.php\/wp-json\/wp\/v2\/comments?post=820"}],"version-history":[{"count":11,"href":"https:\/\/learnmobiledevelopment.com\/index.php\/wp-json\/wp\/v2\/posts\/820\/revisions"}],"predecessor-version":[{"id":833,"href":"https:\/\/learnmobiledevelopment.com\/index.php\/wp-json\/wp\/v2\/posts\/820\/revisions\/833"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/learnmobiledevelopment.com\/index.php\/wp-json\/wp\/v2\/media\/831"}],"wp:attachment":[{"href":"https:\/\/learnmobiledevelopment.com\/index.php\/wp-json\/wp\/v2\/media?parent=820"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnmobiledevelopment.com\/index.php\/wp-json\/wp\/v2\/categories?post=820"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnmobiledevelopment.com\/index.php\/wp-json\/wp\/v2\/tags?post=820"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}