

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. The code above handles toggling between dark and light according to the user’s preference in the app, and then switching between user preference and OS theme preference. Private bool _useDeviceThemeSettings = true Īpp.Current.UserAppTheme = OSAppTheme.Unspecified UseDarkMode = UseDeviceThemeSettings = false Īpp.Current.UserAppTheme = OSAppTheme.Light UseLightMode = UseDeviceThemeSettings = false Īpp.Current.UserAppTheme = OSAppTheme.Dark Then add the public properties to the BindingContext for the page, in this case the page handles its own state. Now create the above experience, add three checkboxes to your UI for default, dark, and light. Then, to reset the app to respond automatically to OS theme changes, you can set it back to “Unspecified”: = OSAppTheme.Unspecified To do this, you need only to provide a way to set the like this: = OSAppTheme.Dark

Sometime you may want to give the app user control over the theme instead of relying on the OS theme. You can of course refactor these to styles like this: Īnd if you wish to use predefined color styles, it might look like this: #1d1d1d Updating just the header, this looks like: First enable this preview feature by adding the flag to your : public App()ĭevice.SetFlags(new string) To now control the colors for the dark and light of the head and text, you can replace the static colors with an AppThemeBinding that will react at runtime to the OS theme settings. Contrast that with the header which remains blue and the header text that remains white. When you toggle the iOS simulator between dark and light modes (CMD+SHFT+A) you can see the ContentPage background shift from white to black, and the text from black to white. For example, look at how this new “Blank App” template looks on iOS: When you set no styles or colors, your UI will default to the theme native to the platform the app runs on. 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. In fact, if you do nothing at all, your Xamarin.Forms apps will respect the user’s OS preference. 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.
