The ThemeService
allows you to change the theme of your application at runtime. It provides a set of methods to change the theme, get the current theme, and listen to theme changes.
Console log
The Radzen.Blazor library provides a built-in service that persists the current theme in a cookie. This means that the theme will be remembered even after the user closes the browser or navigates to a different page. The theme will be restored when the user returns to the application.
Program.cs
file(s) and register the CookieThemeService
:
builder.Services.AddRadzenCookieThemeService(options =>
{
options.Name = "MyApplicationTheme"; // The name of the cookie
options.Duration = TimeSpan.FromDays(365); // The duration of the cookie
});
MainLayout.razor
and inject the CookieThemeService
:@inject CookieThemeService CookieThemeService
App.razor
file of your application and add this code:@code {
[CascadingParameter]
private HttpContext HttpContext { get; set; }
[Inject]
private ThemeService ThemeService { get; set; }
protected override void OnInitialized()
{
base.OnInitialized();
if (HttpContext != null)
{
var theme = HttpContext.Request.Cookies["MyApplicationTheme"];
if (!string.IsNullOrEmpty(theme))
{
ThemeService.SetTheme(theme, false);
}
}
}
}
Radzen Blazor Studio is a software development environment that empowers developers to design, build and deploy Blazor applications without the traditional hurdles. Write less code and get more done.
Radzen Blazor Components, © 2018-2024 Radzen.
Source Code licensed under
MIT