Implement The Dark Mode in Xamarin.Forms
4 minute(s) read | Published on: Sep 20, 2021 Updated on: Dec 14, 2021 |
The style and theme of the applications were designed and developed by Xamarin.Forms ( in French: les applications conçues et développées par Xamarin.Forms ) can be changed. Application themes can be set to dark and light. Of course, application themes can also change when the program and developed apps are running.
How to change the theme of applications at runtime?
The process of changing the theme of applications at runtime using Xamarin.Forms are as follows:
- In ResourceDictionary, you can enter the resources of each topic.
- DynamicResource markup plugins can be used to use these resources.
- You can set the default theme of the developed application to App.xaml.
- Codes related to changing the theme's time to run the application must also be entered.
Change the theme of applications.
Application themes are stored and defined as resource objects in ResourceDictionary.
The following example examples and code can be used to represent LightTheme: (C #)
The following example examples and code can be used to represent DarkTheme: (C #)
How to set the default theme in applications
It should be noted that every application must have a default theme before being set by default for applications. You can specify pieces by merging ResourceDictionary in ResourceDictionary.
The code and contents should be included in the App. XAML file is as follows: (C #)
Theme Resources
DynamicResource must be used when the application wants to use the resources stored in ResourceDictionary to change the theme and style of the application.
The example and code below can show three styles and three themes that can be set on the Label object: (C #)
It should be noted that all these styles and themes are defined in the application-level resource dictionary so that they can be used on several different pages. Each type can use DynamicResource markup to consume these resources. (C #)
DynamicResource markup plugins must be used when the subject resources are used directly and consumed.
How to change and load the theme when running and using the application
To do this, you must:
- Remove the current theme from the application. You can do this by clearing the MergedDictionaries feature from the ResourceDictionary level.
- Then, you must upload the selected theme and the theme that you want to change at runtime. You can also do this by selecting an instance and adding it to the MergedDictionaries feature at the ResourceDictionary level.
The following code from the OnPickerSelectionChanged method can be used to modify and load the newly selected theme (C #):
Below, we will insert the steps carefully to apply the dark theme to the applications.
The prerequisites for doing this are as follows.
- Visual Studio 2017 or higher (Windows or Mac)
- Xamarin.Forms project
1- We will create a project in Xamarin. Forms to change and apply a dark theme to the application.
2- Then, I use a new form of Xamarin to create a cloud or use the existing structures.
3- We must create a ResourceDictionary for both light themes and arrow themes.
4- The codes that should be in the DarkTheme.XAML sections are as follows.
5- The codes that should be in the LightTheme.XAML section for the light theme are as follows:
6- In the App. xaml file, we must select the default application theme.
7- The codes and contents of the App. XAML sections are as follows.
8- We need to create an Enum in App.XAML.cs.
9- In addition to them, we must create an interface for collecting themes at runtime and change them.
10- It is better to insert the following codes in the ThemeHelper.cs section to change the application's theme at runtime. ( Designed and developed for applications by the iOS operating system.)
11- It is better to insert the following codes in the ThemeHelper.cs section to change the application's theme at runtime. ( Designed and developed for Android applications.)
How to use styles
12- DynamicResource BackgroundColor can consume styles in applications as follows.
Using the codes listed above and following the steps carefully, you can change the application's theme at runtime. The codes listed above are for changing the composition of Android and iOS operating system applications.
About our Android and iOS app development services