{"id":47525,"date":"2020-07-16T11:49:36","date_gmt":"2020-07-16T18:49:36","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/xamarin\/?p=47525"},"modified":"2020-07-17T08:17:06","modified_gmt":"2020-07-17T15:17:06","slug":"app-themes-xamarin-forms","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/app-themes-xamarin-forms\/","title":{"rendered":"App Themes for Xamarin.Forms"},"content":{"rendered":"<p>All major OSes now support dark and light app themes, and Xamarin.Forms 4.7 has arrived to make this easy to add to your applications. In fact, if you do nothing at all, your Xamarin.Forms apps will respect the user&#8217;s OS preference. Why stop there? You can also customize the light and dark colors used throughout your app UI, and even give the user a choice to control their own app theme. Let&#8217;s start at the beginning.<\/p>\n<h2>Default Platform Colors<\/h2>\n<p>When you set no styles or colors, your UI will default to the theme native to the platform the app runs on. For example, look at how this new &#8220;Blank App&#8221; template looks on iOS:<\/p>\n<pre><code class=\"xml\">&lt;StackLayout&gt;\n            &lt;Frame BackgroundColor=\"#2196F3\" Padding=\"36,48,36,36\" CornerRadius=\"0\"&gt;\n                &lt;Label Text=\"Welcome to Xamarin.Forms!\" HorizontalTextAlignment=\"Center\" TextColor=\"White\" FontSize=\"36\" \/&gt;\n            &lt;\/Frame&gt;\n            &lt;Label Text=\"Start developing now\" FontSize=\"Title\" Padding=\"30,10,30,10\" \/&gt;\n            &lt;Label Text=\"Make changes to your XAML file and save to see your UI update in the running app with XAML Hot Reload. Give it a try!\" FontSize=\"16\" Padding=\"30,0,30,0\" \/&gt;\n            &lt;Label FontSize=\"16\" Padding=\"30,24,30,0\"&gt;\n                &lt;Label.FormattedText&gt;\n                    &lt;FormattedString&gt;\n                        &lt;FormattedString.Spans&gt;\n                            &lt;Span Text=\"Learn more at \" \/&gt;\n                            &lt;Span Text=\"https:\/\/aka.ms\/xamarin-quickstart\" FontAttributes=\"Bold\" \/&gt;\n                        &lt;\/FormattedString.Spans&gt;\n                    &lt;\/FormattedString&gt;\n                &lt;\/Label.FormattedText&gt;\n            &lt;\/Label&gt;\n        &lt;\/StackLayout&gt;\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/07\/apptheme-default.gif\" alt=\"Image apptheme default\" width=\"352\" height=\"700\" class=\"aligncenter size-full wp-image-47526\" \/><\/p>\n<p>When you toggle the iOS simulator between dark and light modes (CMD+SHFT+A) you can see the <code>ContentPage<\/code> background shift from white to black, and the text from black to white. Those are default platform colors. Contrast that with the header which remains blue and the header text that remains white. Those are explicit colors set in code.<\/p>\n<h2>Take Control of the Dark<\/h2>\n<p>To now control the colors for the dark and light of the head and text, you can replace the static colors with an <code>AppThemeBinding<\/code> that will react at runtime to the OS theme settings. First enable this preview feature by adding the flag to your App.xaml.cs:<\/p>\n<pre><code class=\"csharp\">public App()\n{\n    Device.SetFlags(new string[]{ \"AppTheme_Experimental\" });\n\n    InitializeComponent();\n}\n<\/code><\/pre>\n<p>Updating just the header, this looks like:<\/p>\n<pre><code class=\"xml\">&lt;Frame BackgroundColor=\"{AppThemeBinding Dark=#2196F3, Light=#2196F3}\" Padding=\"36,48,36,36\" CornerRadius=\"0\"&gt;\n                &lt;Label Text=\"Welcome to Xamarin.Forms!\" HorizontalTextAlignment=\"Center\" TextColor=\"{AppThemeBinding Dark=DarkBlue, Light=White}\" FontSize=\"36\" \/&gt;\n            &lt;\/Frame&gt;\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/07\/apptheme-explicit.gif\" alt=\"Image apptheme explicit\" width=\"407\" height=\"339\" class=\"aligncenter size-full wp-image-47527\" \/><\/p>\n<p>You can of course refactor these to styles like this:<\/p>\n<pre><code class=\"xml\">&lt;ContentPage.Resources&gt;\n    &lt;Style x:Key=\"HeaderBg\" TargetType=\"Frame\"&gt;\n        &lt;Setter Property=\"BackgroundColor\" Value=\"{AppThemeBinding Dark=#1d1d1d, Light=#2196F3}\"\/&gt;\n        &lt;Setter Property=\"Padding\" Value=\"36,48,36,36\"\/&gt;\n        &lt;Setter Property=\"CornerRadius\" Value=\"0\"\/&gt;\n    &lt;\/Style&gt;\n\n    &lt;Style x:Key=\"HeaderTitle\" TargetType=\"Label\"&gt;\n        &lt;Setter Property=\"TextColor\" Value=\"{AppThemeBinding Dark=#F1F1F1, Light=White}\"\/&gt;\n        &lt;Setter Property=\"HorizontalTextAlignment\" Value=\"Center\"\/&gt;\n        &lt;Setter Property=\"FontSize\" Value=\"36\"\/&gt;\n    &lt;\/Style&gt;\n&lt;\/ContentPage.Resources&gt;\n\n&lt;Frame Style=\"{StaticResource HeaderBg}\"&gt;\n    &lt;Label\n        Style=\"{StaticResource HeaderTitle}\"\n        Text=\"Welcome to Xamarin.Forms!\" \/&gt;\n&lt;\/Frame&gt;\n<\/code><\/pre>\n<p>And if you wish to use predefined color styles, it might look like this:<\/p>\n<pre><code class=\"xml\">&lt;Color x:Key=\"Background_Dark\"&gt;#1d1d1d&lt;\/Color&gt;\n&lt;Color x:Key=\"Background_Light\"&gt;#1d1d1d&lt;\/Color&gt;\n&lt;Style x:Key=\"HeaderBg\" TargetType=\"Frame\"&gt;\n    &lt;Setter Property=\"BackgroundColor\" Value=\"{AppThemeBinding Dark={StaticResource Background_Dark}, Light={StaticResource Background_Light}}\"\/&gt;\n<\/code><\/pre>\n<h2>Letting the User Choose<\/h2>\n<p>Sometime you may want to give the app user control over the theme instead of relying on the OS theme. To do this, you need only to provide a way to set the <code>App.Current.UserAppTheme<\/code> like this:<\/p>\n<pre><code class=\"csharp\">App.Current.UserAppTheme = OSAppTheme.Dark;\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/07\/apptheme-user.gif\" alt=\"app themes\" width=\"466\" height=\"500\" class=\"aligncenter size-full wp-image-47528\" \/><\/p>\n<p>Then, to reset the app to respond automatically to OS theme changes, you can set it back to &#8220;Unspecified&#8221;:<\/p>\n<pre><code class=\"csharp\">App.Current.UserAppTheme = OSAppTheme.Unspecified;\n<\/code><\/pre>\n<p>Now create the above experience, add three checkboxes to your UI for default, dark, and light.<\/p>\n<pre><code class=\"xml\">&lt;StackLayout Orientation=\"Horizontal\" Spacing=\"10\"&gt;\n    &lt;CheckBox IsChecked=\"{Binding UseDeviceThemeSettings}\" VerticalOptions=\"Center\" \/&gt;\n    &lt;Label Text=\"Use device settings\"\n            VerticalOptions=\"Center\"\/&gt;\n&lt;\/StackLayout&gt;\n\n&lt;StackLayout\n    IsVisible=\"{Binding UseDeviceThemeSettings, Converter={StaticResource InvertedBoolConverter}}\"\n    Orientation=\"Horizontal\"\n    Spacing=\"10\"&gt;\n    &lt;CheckBox IsChecked=\"{Binding UseDarkMode}\" VerticalOptions=\"Center\"\/&gt;\n    &lt;Label Text=\"Dark Theme\"\n            VerticalOptions=\"Center\"\/&gt;\n&lt;\/StackLayout&gt;\n\n&lt;StackLayout\n    IsVisible=\"{Binding UseDeviceThemeSettings, Converter={StaticResource InvertedBoolConverter}}\"\n    Orientation=\"Horizontal\"\n    Spacing=\"10\"&gt;\n    &lt;CheckBox IsChecked=\"{Binding UseLightMode}\" VerticalOptions=\"Center\"\/&gt;\n    &lt;Label Text=\"Light Theme\"\n            VerticalOptions=\"Center\"\/&gt;\n&lt;\/StackLayout&gt;\n<\/code><\/pre>\n<p>Then add the public properties to the BindingContext for the page, in this case the page handles its own state.<\/p>\n<pre><code class=\"csharp\">public MainPage()\n{\n    BindingContext = this;\n    InitializeComponent();\n}\n\nprivate bool _useDarkMode;\npublic bool UseDarkMode {\n    get {\n        return _useDarkMode;\n    }\n    set {\n        _useDarkMode = value;\n        if(_useDarkMode)\n        {\n            UseLightMode = UseDeviceThemeSettings = false;\n            App.Current.UserAppTheme = OSAppTheme.Dark;\n        }\n\n    }\n}\n\nprivate bool _useLightMode;\npublic bool UseLightMode\n{\n    get\n    {\n        return _useLightMode;\n    }\n    set\n    {\n        _useLightMode = value;\n        if (_useLightMode)\n        {\n            UseDarkMode = UseDeviceThemeSettings = false;\n            App.Current.UserAppTheme = OSAppTheme.Light;\n        }\n    }\n}\n\nprivate bool _useDeviceThemeSettings = true;\npublic bool UseDeviceThemeSettings\n{\n    get\n    {\n        return _useDeviceThemeSettings;\n    }\n    set\n    {\n        _useDeviceThemeSettings = value;\n        if(_useDeviceThemeSettings)\n        {\n            App.Current.UserAppTheme = OSAppTheme.Unspecified;\n        }\n    }\n\n}\n<\/code><\/pre>\n<p>The code above handles toggling between dark and light according to the user&#8217;s preference in the app, and then switching between user preference and OS theme preference.<\/p>\n<h2>That&#8217;s a Wrap<\/h2>\n<p>This new dark and light mode theme helper, <code>AppThemeBinding<\/code> along with <code>UserAppTheme<\/code> make it really easy to handle theme modes in your Xamarin.Forms apps. This works not only for colors, but images and other resources as well. As shown previously, it even works with the new Shapes and Paths introduced in Xamarin.Forms 4.7 too!<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/07\/apptheme-shapes.gif\" alt=\"Image apptheme shapes\" width=\"353\" height=\"700\" class=\"aligncenter size-full wp-image-47529\" \/><\/p>\n<p>For more information on app themes, check out the <a href=\"https:\/\/docs.microsoft.com\/xamarin\/xamarin-forms\/user-interface\/theming\/system-theme-changes\">system theme changes documentation<\/a>. If you want to go even further than dark and light themes, then check out using dynamic resources and even loading themes at runtime to <a href=\"https:\/\/docs.microsoft.com\/xamarin\/xamarin-forms\/user-interface\/theming\/theming\">theme your applications<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This new dark and light mode theme helper, AppThemeBinding along with UserAppTheme make it really easy to handle theme modes in your Xamarin.Forms apps. <\/p>\n","protected":false},"author":553,"featured_media":47528,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1,291,367,7745],"tags":[9011,9010,27,9012,16],"class_list":["post-47525","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-xamarin","category-xamarin-platform","category-xamarin-forms","category-xaml","tag-app-themes","tag-mobile-applications","tag-xamarin","tag-xamarin-developers","tag-xamarin-forms"],"acf":[],"blog_post_summary":"<p>This new dark and light mode theme helper, AppThemeBinding along with UserAppTheme make it really easy to handle theme modes in your Xamarin.Forms apps. <\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/47525","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/553"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=47525"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/47525\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media\/47528"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=47525"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=47525"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=47525"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}