英文:
MudBlazor custom color theme
问题
I'm trying to create my custom color theme for a Blazor WASM page where I use MudBlazor. In the MainLayout.razor
file I added
<MudThemeProvider @bind-IsDarkMode="@_darkModeOn" Theme="@_themeDefinition" />
and then in the C# code section I initialize the theme field:
MudTheme _themeDefinition = new GreenYellowTheme();
The GreenYellowTheme.cs file contains the theme (with light and dark mode):
using MudBlazor;
namespace MudBlazorTest.ColorPalettes
{
public class GreenYellowTheme : MudTheme
{
public PaletteLight PaletteLight { get; } = new PaletteLight
{
Black = "#000000",
White = "#FFFFFF",
Primary = "#FF0000",
Secondary = "#00FF00",
Tertiary = "#0000FF",
Success = "#00FFFF",
Info = "#FFFF00",
Warning = "#FF00FF",
Error = "#C0C0C0",
Dark = "#333333",
Background = "#FFFFFF",
};
public PaletteDark PaletteDark { get; } = new PaletteDark
{
Black = "#000000",
White = "#FFFFFF",
Primary = "#FFFFFF", //white
Secondary = "#c5b858", // yellow
Tertiary = "#1b5e20", // green
Success = "#00FFFF",
Info = "#FFFF00",
Warning = "#FF00FF",
Error = "#C0C0C0",
Dark = "#303030",
Background = "#303030",
};
}
}
Unfortunately, when I start the application I see the stock palette from MudBlazor instead of my custom one. What am I doing wrong?
英文:
I'm trying to create my custom color theme for a Blazor WASM page where I use MudBlazor. In the MainLayout.razor
file I added
<MudThemeProvider @bind-IsDarkMode="@_darkModeOn" Theme="@_themeDefinition" />
and then in the C# code section I initialize the theme field:
MudTheme _themeDefinition = new GreenYellowTheme();
The GreenYellowTheme.cs file contains the theme (with light and dark mode):
using MudBlazor;
namespace MudBlazorTest.ColorPalettes
{
public class GreenYellowTheme : MudTheme
{
public PaletteLight PaletteLight { get; } = new PaletteLight
{
Black = "#000000",
White = "#FFFFFF",
Primary = "#FF0000",
Secondary = "#00FF00",
Tertiary = "#0000FF",
Success = "#00FFFF",
Info = "#FFFF00",
Warning = "#FF00FF",
Error = "#C0C0C0",
Dark = "#333333",
Background = "#FFFFFF",
};
public PaletteDark PaletteDark { get; } = new PaletteDark
{
Black = "#000000",
White = "#FFFFFF",
Primary = "#FFFFFF", //white
Secondary = "#c5b858", // yellow
Tertiary = "#1b5e20", // green
Success = "#00FFFF",
Info = "#FFFF00",
Warning = "#FF00FF",
Error = "#C0C0C0",
Dark = "#303030",
Background = "#303030",
};
}
}
Unfortunately, when I start the application I see the stock palette from MudBlazor instead of my custom one. What am I doing wrong?
答案1
得分: 1
有两个问题,首先属性应该叫做 Palette
而不是 PaletteLight
,另一个问题是属性 PaletteDark
被重新声明并替换了原来的属性。
相反,你可以使用构造函数将调色板分配给已继承的现有属性:
public class GreenYellowTheme : MudTheme
{
public GreenYellowTheme()
{
Palette = new PaletteLight
{
Black = "#000000",
White = "#FFFFFF",
Primary = "#FF0000",
Secondary = "#00FF00",
Tertiary = "#0000FF",
Success = "#00FFFF",
Info = "#FFFF00",
Warning = "#FF00FF",
Error = "#C0C0C0",
Dark = "#333333",
Background = "#FFFFFF",
};
PaletteDark = new PaletteDark
{
Black = "#000000",
White = "#FFFFFF",
Primary = "#FFFFFF", //white
Secondary = "#c5b858", // yellow
Tertiary = "#1b5e20", // green
Success = "#00FFFF",
Info = "#FFFF00",
Warning = "#FF00FF",
Error = "#C0C0C0",
Dark = "#303030",
Background = "#303030",
};
}
}
英文:
There are two problems I can see, first the property is called Palette
instead of PaletteLight
and the other problem is that the property PaletteDark
is redeclared and replaces the original property.
Instead you could use the constructor to assign the palettes to the already inherited existing properties:
public class GreenYellowTheme : MudTheme
{
public GreenYellowTheme()
{
Palette = new PaletteLight
{
Black = "#000000",
White = "#FFFFFF",
Primary = "#FF0000",
Secondary = "#00FF00",
Tertiary = "#0000FF",
Success = "#00FFFF",
Info = "#FFFF00",
Warning = "#FF00FF",
Error = "#C0C0C0",
Dark = "#333333",
Background = "#FFFFFF",
};
PaletteDark = new PaletteDark
{
Black = "#000000",
White = "#FFFFFF",
Primary = "#FFFFFF", //white
Secondary = "#c5b858", // yellow
Tertiary = "#1b5e20", // green
Success = "#00FFFF",
Info = "#FFFF00",
Warning = "#FF00FF",
Error = "#C0C0C0",
Dark = "#303030",
Background = "#303030",
};
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论