New to Telerik UI for .NET MAUIStart a free 30-day trial

Changing Theme Swatch at Runtime in C# Code

Updated over 6 months ago

Environment

VersionProductAuthor
11.0.0Telerik UI for .NET MAUIDobrinka 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.

  1. 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());
        }
    }
  1. Invoke the method to change the theme and swatch during runtime.
csharp
TelerikThemeResources.AppTheme = TelerikTheme.TelerikTurquoise;
TelerikThemeResources.Current.MergedDictionaries.Add(new CustomTelerikSwatch());

See Also