New to Telerik UI for .NET MAUI? Start a free 30-day trial
Changing Theme Swatch at Runtime in C# Code
Updated over 6 months ago
Environment
| Version | Product | Author |
|---|---|---|
| 11.0.0 | Telerik UI for .NET MAUI | Dobrinka Yordanova |
Description
I want to change the Theme Colors (Swatches) at runtime using C# code in a .NET MAUI application.
This knowledge base article also answers the following questions:
- How to switch theme colors at runtime in .NET MAUI?
- How to dynamically apply theme swatches using C# in Telerik UI for .NET MAUI?
Solution
To change the theme swatch dynamically, set the TelerikThemeResources.AppTheme to the desired pre-defined theme.
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.
- Apply the custom swatch at runtime:
csharp
public partial class App : Application
{
public App()
{
InitializeComponent();
TelerikThemeResources.AppTheme = TelerikTheme.TelerikPurple;
}
protected override Window CreateWindow(IActivationState? activationState)
{
return new Window(new AppShell());
}
}
- Invoke the method to change the theme and swatch during runtime.
csharp
TelerikThemeResources.AppTheme = TelerikTheme.TelerikTurquoise;
TelerikThemeResources.Current.MergedDictionaries.Add(new CustomTelerikSwatch());