|
| 1 | +--- |
| 2 | +title: Changing Theme Swatch at Runtime in C# Code |
| 3 | +description: Learn how to change the Theme Colors (Swatches) at runtime using C# code. |
| 4 | +type: how-to |
| 5 | +page_title: Modify Theme Swatch Dynamically in .NET MAUI |
| 6 | +meta_title: Modify Theme Swatch Dynamically in .NET MAUI |
| 7 | +slug: change-theme-swatch-runtime-dotnet-maui |
| 8 | +tags: teleriktheme, telerikthemresources, apptheme, customswatch, netmaui |
| 9 | +res_type: kb |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +| Version | Product | Author | |
| 15 | +| --- | --- | ---- | |
| 16 | +| 11.0.0 | Telerik UI for .NET MAUI | [Dobrinka Yordanova](https://www.telerik.com/blogs/author/dobrinka-yordanova) | |
| 17 | + |
| 18 | +## Description |
| 19 | + |
| 20 | +I want to change the Theme Colors (Swatches) at runtime using C# code in a .NET MAUI application. |
| 21 | + |
| 22 | +This knowledge base article also answers the following questions: |
| 23 | +- How to switch theme colors at runtime in .NET MAUI? |
| 24 | +- How to dynamically apply theme swatches using C# in Telerik UI for .NET MAUI? |
| 25 | + |
| 26 | +## Solution |
| 27 | + |
| 28 | +To change the theme swatch dynamically, set the `TelerikThemeResources.AppTheme` to the desired pre-defined theme. |
| 29 | + |
| 30 | +Below is an example, showing how to switch from the `TelerikPurple` theme to the `TelerikTurquoise` theme and merge a custom swatch defined in a `CustomTelerikSwatch.xaml` file. |
| 31 | + |
| 32 | +1. Apply the custom swatch at runtime: |
| 33 | + |
| 34 | +```csharp |
| 35 | +public partial class App : Application |
| 36 | + { |
| 37 | + public App() |
| 38 | + { |
| 39 | + InitializeComponent(); |
| 40 | + TelerikThemeResources.AppTheme = TelerikTheme.TelerikPurple; |
| 41 | + } |
| 42 | + |
| 43 | + protected override Window CreateWindow(IActivationState? activationState) |
| 44 | + { |
| 45 | + return new Window(new AppShell()); |
| 46 | + } |
| 47 | + } |
| 48 | +``` |
| 49 | + |
| 50 | +2. Invoke the method to change the theme and swatch during runtime. |
| 51 | + |
| 52 | +```csharp |
| 53 | +TelerikThemeResources.AppTheme = TelerikTheme.TelerikTurquoise; |
| 54 | +TelerikThemeResources.Current.MergedDictionaries.Add(new CustomTelerikSwatch()); |
| 55 | +``` |
| 56 | + |
| 57 | +## See Also |
| 58 | + |
| 59 | +- [Styling and Themes - Customize the Theme Colors (Swatches)](https://docs.telerik.com/devtools/maui/styling-and-themes/customize-the-theme#customizing-the-theme-colors-swatches) |
| 60 | +- [Change Telerik Theme at Runtime](https://docs.telerik.com/devtools/maui/knowledge-base/change-telerik-theme-runtime) |
0 commit comments