MudBlazor自定义颜色主题

huangapple go评论58阅读模式
英文:

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

&lt;MudThemeProvider @bind-IsDarkMode=&quot;@_darkModeOn&quot; Theme=&quot;@_themeDefinition&quot; /&gt;

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 = &quot;#000000&quot;,
            White = &quot;#FFFFFF&quot;,
            Primary = &quot;#FF0000&quot;,
            Secondary = &quot;#00FF00&quot;,
            Tertiary = &quot;#0000FF&quot;,
            Success = &quot;#00FFFF&quot;,
            Info = &quot;#FFFF00&quot;,
            Warning = &quot;#FF00FF&quot;,
            Error = &quot;#C0C0C0&quot;,
            Dark = &quot;#333333&quot;,
            Background = &quot;#FFFFFF&quot;,
        };

        public PaletteDark PaletteDark { get; } = new PaletteDark
        {
            Black = &quot;#000000&quot;,
            White = &quot;#FFFFFF&quot;,
            Primary = &quot;#FFFFFF&quot;, //white
            Secondary = &quot;#c5b858&quot;, // yellow
            Tertiary = &quot;#1b5e20&quot;, // green
            Success = &quot;#00FFFF&quot;,
            Info = &quot;#FFFF00&quot;,
            Warning = &quot;#FF00FF&quot;,
            Error = &quot;#C0C0C0&quot;,
            Dark = &quot;#303030&quot;,
            Background = &quot;#303030&quot;,
        };
    }
}

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 = &quot;#000000&quot;,
            White = &quot;#FFFFFF&quot;,
            Primary = &quot;#FF0000&quot;,
            Secondary = &quot;#00FF00&quot;,
            Tertiary = &quot;#0000FF&quot;,
            Success = &quot;#00FFFF&quot;,
            Info = &quot;#FFFF00&quot;,
            Warning = &quot;#FF00FF&quot;,
            Error = &quot;#C0C0C0&quot;,
            Dark = &quot;#333333&quot;,
            Background = &quot;#FFFFFF&quot;,
        };

        PaletteDark = new PaletteDark
        {
            Black = &quot;#000000&quot;,
            White = &quot;#FFFFFF&quot;,
            Primary = &quot;#FFFFFF&quot;, //white
            Secondary = &quot;#c5b858&quot;, // yellow
            Tertiary = &quot;#1b5e20&quot;, // green
            Success = &quot;#00FFFF&quot;,
            Info = &quot;#FFFF00&quot;,
            Warning = &quot;#FF00FF&quot;,
            Error = &quot;#C0C0C0&quot;,
            Dark = &quot;#303030&quot;,
            Background = &quot;#303030&quot;,
        };
    }
}
英文:

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 = &quot;#000000&quot;,
            White = &quot;#FFFFFF&quot;,
            Primary = &quot;#FF0000&quot;,
            Secondary = &quot;#00FF00&quot;,
            Tertiary = &quot;#0000FF&quot;,
            Success = &quot;#00FFFF&quot;,
            Info = &quot;#FFFF00&quot;,
            Warning = &quot;#FF00FF&quot;,
            Error = &quot;#C0C0C0&quot;,
            Dark = &quot;#333333&quot;,
            Background = &quot;#FFFFFF&quot;,
        };

        PaletteDark = new PaletteDark
        {
            Black = &quot;#000000&quot;,
            White = &quot;#FFFFFF&quot;,
            Primary = &quot;#FFFFFF&quot;, //white
            Secondary = &quot;#c5b858&quot;, // yellow
            Tertiary = &quot;#1b5e20&quot;, // green
            Success = &quot;#00FFFF&quot;,
            Info = &quot;#FFFF00&quot;,
            Warning = &quot;#FF00FF&quot;,
            Error = &quot;#C0C0C0&quot;,
            Dark = &quot;#303030&quot;,
            Background = &quot;#303030&quot;,
        };
    }
}

huangapple
  • 本文由 发表于 2023年6月15日 04:47:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76477434.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定