jd4*_*487 4 c# styling mudblazor
我真的很想更改 MudBlazor UI 组件的颜色。但是,我似乎无法覆盖默认样式。有人可以帮我吗?
Ibr*_*imi 19
如果您想更改默认Mudblazor
颜色,您可以使用自定义Palette
. 这使您可以根据需要更改任何调色板颜色属性。查看下面的示例。
更新:新版本的MudBlazor
框架引入了新的类:PaletteLight
和PaletteDark
。Palette
类现在已经过时了。您可以使用该类PaletteLight
覆盖默认颜色,或使用这两个类为您的应用程序提供浅色和深色主题。
MainLayout.razor
:
@inherits LayoutComponentBase
<MudThemeProvider Theme="_currentTheme"/>
<MudDialogProvider/>
<MudSnackbarProvider/>
<MudLayout>
<MudAppBar Elevation="0">
<MudIconButton Icon="@Icons.Material.Filled.Menu" Color="Color.Inherit" Edge="Edge.Start" OnClick="@(e => DrawerToggle())"/>
<MudSpacer/>
</MudAppBar>
<MudDrawer @bind-Open="_drawerOpen" Elevation="1">
<MudDrawerHeader>
<MudText Typo="Typo.h6">MyApplication</MudText>
</MudDrawerHeader>
<NavMenu/>
</MudDrawer>
<MudMainContent>
<MudContainer MaxWidth="MaxWidth.ExtraLarge" Class="my-8">
@Body
</MudContainer>
</MudMainContent>
</MudLayout>
@code {
bool _drawerOpen = true;
private readonly MudTheme _currentTheme = new()
{
Palette = new PaletteLight
{
Primary = "#0A7BCF",
Secondary = "#4CAF50",
Info = "#64a7e2",
Success = "#2ECC40",
Warning = "#FFC107",
Error = "#FF0000",
AppbarBackground = "#212121",
TextPrimary = "#0A7BCF",
TextSecondary = "#4CAF50",
// more color properties
}
};
void DrawerToggle() => _drawerOpen = !_drawerOpen;
}
Run Code Online (Sandbox Code Playgroud)
MainLayout.razor
:
@inherits LayoutComponentBase
<MudThemeProvider @ref="@_mudThemeProvider" @bind-IsDarkMode="@_isDarkMode" Theme="_currentTheme"/>
<MudDialogProvider/>
<MudSnackbarProvider/>
<MudLayout>
<MudAppBar Elevation="0">
<MudIconButton Icon="@Icons.Material.Filled.Menu" Color="Color.Inherit" Edge="Edge.Start" OnClick="@(e => DrawerToggle())"/>
<MudSpacer/>
<MudIconButton Icon="@Icons.Material.Filled.Brightness4"
Color="Color.Inherit"
Class="nav-button"
OnClick="@ThemeToggle"/>
</MudAppBar>
<MudDrawer @bind-Open="_drawerOpen" Elevation="1">
<MudDrawerHeader>
<MudText Typo="Typo.h6">MyApplication</MudText>
</MudDrawerHeader>
<NavMenu/>
</MudDrawer>
<MudMainContent>
<MudContainer MaxWidth="MaxWidth.ExtraLarge" Class="my-8">
@Body
</MudContainer>
</MudMainContent>
</MudLayout>
@code {
bool _drawerOpen = true;
private bool _isDarkMode;
private MudThemeProvider _mudThemeProvider;
private readonly MudTheme _currentTheme = new()
{
Palette = new PaletteLight
{
Primary = "#0A7BCF",
Secondary = "#4CAF50",
Info = "#64a7e2",
Success = "#2ECC40",
Warning = "#FFC107",
Error = "#FF0000",
AppbarBackground = "#212121",
TextPrimary = "#0A7BCF",
TextSecondary = "#4CAF50",
// more color properties
},
PaletteDark = new PaletteDark
{
Primary = "#6585e0",
Secondary = "#607D8B",
Info = "#a4c2dd",
Success = "#2ECC40",
Warning = "#dc2d7e",
Error = "#de2000",
AppbarBackground = "#121212",
TextPrimary = "#E0E0E0",
TextSecondary = "#BDBDBD",
// more color properties
}
};
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
_isDarkMode = await _mudThemeProvider.GetSystemPreference();
StateHasChanged();
}
}
void DrawerToggle() => _drawerOpen = !_drawerOpen;
void ThemeToggle() => _isDarkMode = !_isDarkMode;
}
Run Code Online (Sandbox Code Playgroud)
Index.razor
:
@page "/"
<PageTitle>Index</PageTitle>
<MudText Typo="Typo.h2" Class="mb-5">Theme Colors</MudText>
<MudStack Row="true">
<MudButton Color="Color.Primary" Variant="Variant.Filled">Primary Button</MudButton>
<MudButton Color="Color.Secondary" Variant="Variant.Filled">Secondary Button</MudButton>
<MudButton Color="Color.Success" Variant="Variant.Filled">Success Button</MudButton>
<MudButton Color="Color.Warning" Variant="Variant.Filled">Warning Button</MudButton>
<MudButton Color="Color.Error" Variant="Variant.Filled">Error Button</MudButton>
</MudStack>
<br/>
<MudAlert Severity="Severity.Normal">The reactor type is RBMK-1000</MudAlert>
<MudAlert Severity="Severity.Info">The reactor was fired up successfully</MudAlert>
<MudAlert Severity="Severity.Success">The reactor is running at optimum temperature</MudAlert>
<MudAlert Severity="Severity.Warning">The reactor temperature exceeds the optimal range</MudAlert>
<MudAlert Severity="Severity.Error">Meltdown is imminent</MudAlert>
Run Code Online (Sandbox Code Playgroud)
深色模式演示:
在我们详细介绍如何使用 CSS 为 MudBlazor 组件设置样式之前,让我向您指出主题文档。
如果主题对您来说还不够,您有多种选择,可以使用 CSS 更改 MudBlazor 元素的样式。请注意,您可能必须申请!important
覆盖 MudBlazor 的默认样式。
一种方法是在主 CSS 文件中定义自己的 CSS 类,并将类名传递给组件,如下所示:
<MudButton Class="my-class">Button with custom class</MudButton>
Run Code Online (Sandbox Code Playgroud)
第二种方法是通过Style
属性直接传递 CSS 样式,就像这里记录的那样
<MudButton Variant="Variant.Filled" EndIcon="@Icons.Material.ArrowDownward" Style="background-color: yellowgreen; color: white; width: 200px; height: 60px;">
Download Now
</MudButton>
Run Code Online (Sandbox Code Playgroud)
另一种方法是<style>
在您的剃刀中嵌入一个标签,您甚至可以在其中使用 C# 变量来动态修改 CSS。您可以在此小提琴中尝试以下示例
<style>
button {
background-color: yellowgreen !important;
color: white !important;
height: @(height)px;
}
</style>
<MudSlider @bind-Value="height" Min="30" Max="300"/>
<MudButton Variant="Variant.Filled">
Use Slider to change my height
</MudButton>
@code {
int height=100;
}
Run Code Online (Sandbox Code Playgroud)
最后但并非最不重要的一点是,如果您想使用 Blazor 的 CSS 隔离,您必须确保您的页面/组件顶级元素是一个 html 元素,并且您::deep
按照此处的讨论使用。这会将组件或页面中所有按钮的文本更改为红色:
::deep .mud-button-text { color: red !important; }
Run Code Online (Sandbox Code Playgroud)
如果您想要更改整个颜色主题,但同时想要保持应用颜色组合的时间和方式的一致性(根据.mud-*
类),您可以--mud-*
为整个解决方案覆盖 MudBlazor 的 CSS 变量 ( )。
例如,当您使用 时<MudButton Color="Color.Primary"> </MudButton>
,您的按钮元素将自动应用类组合.mud-button-text.mud-button-text-primary
。然后,该类组合应用样式:color: var(--mud-palette-primary);
。--mud-palette-primary
通过覆盖根级别的值,您将为整个解决方案更改它;例如,为您的 MudButton 元素生成另一种文本颜色Color.Primary
。
在全局 CSS 文件中,可以按如下方式覆盖 CSS 变量:
:root {
--mud-palette-primary: violet;
--mud-palette-primary-text: yellow;
}
Run Code Online (Sandbox Code Playgroud)
与任何其他 CSS 颜色属性值一样,也可以使用十六进制值。
如果要更改整个调色板,这可能是一项彻底的工作,但不需要自定义类或!important
类。
不利的一面是,MudBlazor 肯定会在新版本中改变他们对 CSS 变量的使用(可能还有他们的选择);因此,随着时间的推移,这个变量覆盖将需要维护。(话又说回来,我能想到的任何其他可能的解决方案也是如此。)
可在此处快速尝试的示例片段。
(注意:在示例代码片段中,Variant
除了 之外,还在 MudButton 元素上设置了该属性Color
;这设置了一组不同的类,这又以不同的方式使用 MudBlazor CSS 变量应用样式。)
归档时间: |
|
查看次数: |
1737 次 |
最近记录: |