Introduction

Xamarin.Forms code runs on multiple platforms - each of which has its own filesystem. This means that reading and writing files is most easily done using the native file APIs on each platform. Alternatively, embedded resources are a simpler solution to distribute data files with an app.

Dark Mode

Nowadays iOS and Android apps should support both Dark and Light Theme.
Prerequisites
  • Visual Studio 2017 or later (Windows or Mac)

Setting up a Xamarin.Forms Project

Start by creating a new Xamarin.Forms project. You wíll learn more by going through the steps yourself.
Create a new or existing Xamarin forms(.Net standard) Project. With Android and iOS Platform.

Create a Theme

Create a ResourceDictionary for both Light and Dark Theme.
DarkTheme.xaml
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  4. x:Class="DarkModePOC.Common.Styles.DarkTheme">
  5. <Color x:Key="BackgroundColor">#FF000000</Color>
  6. <Color x:Key="FrameColor">#FF1f1f1f</Color>
  7. <Color x:Key="TextPrimaryColor">#B0FFFFFF</Color>
  8. <Color x:Key="TextSecondaryColor">#eFeFeF</Color>
  9. <Style x:Key="Title" TargetType="Label">
  10. <Setter Property="TextColor" Value="{StaticResource TextPrimaryColor}"/>
  11. <Setter Property="FontAttributes" Value="Bold"/>
  12. <Setter Property="FontSize" Value="Large"/>
  13. </Style>
  14. </ResourceDictionary>
LightTheme.xaml
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  4. x:Class="DarkModePOC.Common.Styles.LightTheme">
  5. <Color x:Key="BackgroundColor">#FFe0e0e0</Color>
  6. <Color x:Key="FrameColor">#FFFFFFFF</Color>
  7. <Color x:Key="TextPrimaryColor">#B0000000</Color>
  8. <Color x:Key="TextSecondaryColor">#858585</Color>
  9. <Style x:Key="Title" TargetType="Label">
  10. <Setter Property="TextColor" Value="{StaticResource TextPrimaryColor}"/>
  11. <Setter Property="FontSize" Value="Large"/>
  12. </Style>
  13. </ResourceDictionary>

Set App Theme

In app.xaml you can set the default theme.
App.xaml
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <Application xmlns="http://xamarin.com/schemas/2014/forms"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  4. xmlns:d="http://xamarin.com/schemas/2014/forms/design"
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6. mc:Ignorable="d"
  7. x:Class="DarkModePOC.App">
  8. <Application.Resources>
  9. <ResourceDictionary Source="Common/Styles/LightTheme.xaml" />
  10. </Application.Resources>
  11. </Application>
Now, Create a Enum in App.xaml.cs
  1. public partial class App : Application
  2. {
  3. public static Theme AppTheme { get; set; }
  4. public App()
  5. {
  6. InitializeComponent();
  7. MainPage = new NavigationPage(new MainPage());
  8. }
  9. protected override void OnStart()
  10. {
  11. }
  12. protected override void OnSleep()
  13. {
  14. }
  15. protected override void OnResume()
  16. {
  17. }
  18. public enum Theme
  19. {
  20. Light,
  21. Dark
  22. }
  23. }

Create a Interface

Create a interface for set Theme at runtime.
  1. public interface IAppTheme
  2. {
  3. void SetAppTheme(Theme theme);
  4. }
iOS Implementation
Below code to change the Theme at runtime.
ThemeHelper.cs
  1. [assembly:Dependency(typeof(DarkModePOC.iOS.ThemeHelper))]
  2. namespace DarkModePOC.iOS
  3. {
  4. public class ThemeHelper : IAppTheme
  5. {
  6. public void SetAppTheme(App.Theme theme)
  7. {
  8. SetTheme(theme);
  9. }
  10. void SetTheme(Theme mode)
  11. {
  12. if (mode == Theme.Dark)
  13. {
  14. if (App.AppTheme == Theme.Dark)
  15. return;
  16. App.Current.Resources = new DarkTheme();
  17. }
  18. else
  19. {
  20. if (App.AppTheme != Theme.Dark)
  21. return;
  22. App.Current.Resources = new LightTheme();
  23. }
  24. App.AppTheme = mode;
  25. }
  26. }
  27. }
Android Implementation
Below code to change the Theme at runtime.
ThemeHelper.cs
  1. [assembly: Dependency(typeof(DarkModePOC.Droid.ThemeHelper))]
  2. namespace DarkModePOC.Droid
  3. {
  4. public class ThemeHelper : IAppTheme
  5. {
  6. public void SetAppTheme(App.Theme theme)
  7. {
  8. SetTheme(theme);
  9. }
  10. void SetTheme(Theme mode)
  11. {
  12. if (mode == Theme.Dark)
  13. {
  14. if (App.AppTheme == Theme.Dark)
  15. return;
  16. App.Current.Resources = new DarkTheme();
  17. }
  18. else
  19. {
  20. if (App.AppTheme != Theme.Dark)
  21. return;
  22. App.Current.Resources = new LightTheme();
  23. }
  24. App.AppTheme = mode;
  25. }
  26. }
  27. }
Consuming the Styles
Now, you can use the resource "DynamicResource BackgroundColor" like this.
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  4. xmlns:d="http://xamarin.com/schemas/2014/forms/design"
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6. mc:Ignorable="d"
  7. BackgroundColor="{DynamicResource BackgroundColor}"
  8. x:Class="DarkModePOC.MainPage">
  9. <StackLayout HorizontalOptions="Center" VerticalOptions="Center">
  10. <!-- Place new controls here -->
  11. <Switch x:Name="themeToggle" IsToggled="False" Toggled="Switch_Toggled"/>
  12. <Label TextColor="{DynamicResource TextSecondaryColor}" Text=" sample"/>
  13. <Button HorizontalOptions="Center" VerticalOptions="Center" TextColor="Red" Text="Next Page" Clicked="Button_Clicked" />
  14. </StackLayout>
  15. </ContentPage>
MainPage.Xaml.cs
  1. private void Switch_Toggled(object sender, ToggledEventArgs e)
  2. {
  3. var toggleStatus = themeToggle.IsToggled;
  4. SetTheme(toggleStatus);
  5. }
  6. void SetTheme(bool status)
  7. {
  8. Theme themeRequested;
  9. if (status)
  10. {
  11. themeRequested = Theme.Dark;
  12. }
  13. else
  14. {
  15. themeRequested = Theme.Light;
  16. }
  17. DependencyService.Get<IAppTheme>().SetAppTheme(themeRequested);
  18. }
Run - DarkMode
LightMode
I hope you have understood how to support Dark Mode in Xamarin.Forms..
Thanks for reading. Please share your comments and feedback.
Happy Coding :)