|
1 |
| -# -How-to-Automatically-Switch-Syncfusion-Control-Themes-Based-on-Device-Selected-Theme-in-.NET-MAUI |
2 |
| -This repository contains a sample explaining how to automatically switch Syncfusion control themes based on device selected theme in .NET MAUI. Tokens: maui, theme, device-theme, Syncfusion-theme |
| 1 | +This article explains how to automatically switch [.NET MAUI Syncfusion control](https://www.syncfusion.com/maui-controls) themes based on the device-selected theme. This can be achieved by using [SyncfusionThemeResourceDictionary](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.Themes.SyncfusionThemeResourceDictionary.html). |
| 2 | + |
| 3 | +To enable automatic theme switching for Syncfusion controls based on the device's selected theme in a .NET MAUI application, you can utilize the `OnAppearing` method to assign the Syncfusion [VisualTheme](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.Themes.SyncfusionThemeResourceDictionary.html#Syncfusion_Maui_Themes_SyncfusionThemeResourceDictionary_VisualTheme). Additionally, handling the `RequestedThemeChanged` event allows for dynamic updates to the Syncfusion controls' theme when the device's theme changes at runtime. |
| 4 | + |
| 5 | +**App.xaml Configuration** |
| 6 | + |
| 7 | +Ensure that your App.xaml includes the `SyncfusionThemeResourceDictionary`: |
| 8 | + |
| 9 | +``` |
| 10 | +<Application ... |
| 11 | + xmlns:themes="clr-namespace:Syncfusion.Maui.Themes;assembly=Syncfusion.Maui.Core"> |
| 12 | + <Application.Resources> |
| 13 | + <ResourceDictionary> |
| 14 | + <ResourceDictionary.MergedDictionaries> |
| 15 | + <themes:SyncfusionThemeResourceDictionary /> |
| 16 | + ... |
| 17 | + </ResourceDictionary.MergedDictionaries> |
| 18 | + </ResourceDictionary> |
| 19 | + </Application.Resources> |
| 20 | +</Application> |
| 21 | +``` |
| 22 | + |
| 23 | +**XAML** |
| 24 | + |
| 25 | +``` |
| 26 | +//Mainpage.xaml |
| 27 | +
|
| 28 | +<VerticalStackLayout Padding="30,0" Spacing="25"> |
| 29 | + <buttons:SfButton x:Name="CounterBtn" Text="Click me" Clicked="OnCounterClicked" |
| 30 | + HorizontalOptions="Fill" /> |
| 31 | +</VerticalStackLayout> |
| 32 | +``` |
| 33 | + |
| 34 | +**C#** |
| 35 | + |
| 36 | +1. Override the `OnAppearing` method to apply the current theme and set up an event handler for theme changes. |
| 37 | +2. Implement the `OnRequestedThemeChanged` event handler to respond to theme changes during runtime. |
| 38 | +3. Define the `ApplyTheme` method to update the Syncfusion theme based on the current application theme. |
| 39 | + |
| 40 | +```csharp |
| 41 | +//Mainpage.xaml.cs |
| 42 | +
|
| 43 | +protected override void OnAppearing() |
| 44 | +{ |
| 45 | + if (Application.Current != null) |
| 46 | + { |
| 47 | + this.ApplyTheme(Application.Current.RequestedTheme); |
| 48 | + Application.Current.RequestedThemeChanged += OnRequestedThemeChanged; |
| 49 | + } |
| 50 | + base.OnAppearing(); |
| 51 | +} |
| 52 | + |
| 53 | +private void OnRequestedThemeChanged(object? sender, AppThemeChangedEventArgs e) |
| 54 | +{ |
| 55 | + this.ApplyTheme(e.RequestedTheme); |
| 56 | +} |
| 57 | + |
| 58 | +public void ApplyTheme(AppTheme appTheme) |
| 59 | +{ |
| 60 | + if (Application.Current != null) |
| 61 | + { |
| 62 | + ICollection<ResourceDictionary> mergedDictionaries = Application.Current.Resources.MergedDictionaries; |
| 63 | + if (mergedDictionaries != null) |
| 64 | + { |
| 65 | + var syncTheme = mergedDictionaries.OfType<SyncfusionThemeResourceDictionary>().FirstOrDefault(); |
| 66 | + if (syncTheme != null) |
| 67 | + { |
| 68 | + if (appTheme is AppTheme.Light) |
| 69 | + { |
| 70 | + syncTheme.VisualTheme = SfVisuals.MaterialLight; |
| 71 | + } |
| 72 | + else |
| 73 | + { |
| 74 | + syncTheme.VisualTheme = SfVisuals.MaterialDark; |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +**Output** |
| 83 | + |
| 84 | + |
0 commit comments