Question Switch from light mode to dark and back Blazor

Pablo

Well-known member
Joined
Aug 12, 2021
Messages
92
Programming Experience
10+
Hi,
I'm trying to be able to switch from light mode to dark mode and back to light mode in Blazor.
By now, the only I could do is setting dark mode with adding:

<html lang="en" data-bs-theme="dark">

Any help will be appreciated.
Regards
Pablo
 
Solution
Some interesting answers in StackOverflow:
Thank you!

In which file should be placed this code? (I'm following what you gave me in your reply)

C#:
Expand Collapse Copy
@using BootstrapColourModeDemo.Services
@inject IColorModeServices colorService
@inject NavigationManager navManager

@rendermode InteractiveServer

@page "/"

<PageTitle>Home - Color Mode</PageTitle>

<h1>Bootstrap Color Mode</h1>

<button class="btn btn-primary" @onclick="@(() => SetTheme("light"))"> <i class="bi bi-sun"></i> Light</button>
<button class="btn btn-primary" @onclick="@(() => SetTheme("dark"))"> <i class="bi bi-moon-stars"></i> Dark</button>


@code {

void SetTheme(string theme)
{
    if...
Some interesting answers in StackOverflow:
 
Some interesting answers in StackOverflow:
Thank you!

In which file should be placed this code? (I'm following what you gave me in your reply)

C#:
Expand Collapse Copy
@using BootstrapColourModeDemo.Services
@inject IColorModeServices colorService
@inject NavigationManager navManager

@rendermode InteractiveServer

@page "/"

<PageTitle>Home - Color Mode</PageTitle>

<h1>Bootstrap Color Mode</h1>

<button class="btn btn-primary" @onclick="@(() => SetTheme("light"))"> <i class="bi bi-sun"></i> Light</button>
<button class="btn btn-primary" @onclick="@(() => SetTheme("dark"))"> <i class="bi bi-moon-stars"></i> Dark</button>


@code {

void SetTheme(string theme)
{
    if (colorService.SetColourMode("User1", theme))
    {
        //To change immediately navidate to start of site
        navManager.NavigateTo("/");
    }
}
}

Thank you very much.
Pablo
 
Solution
Back
Top Bottom