我有一個使用 MahApps(“淺色”模式)的 WPF 應用程式,現在我為用戶提供了一種切換模式(淺色/深色)的方法。許多應用程式的顏色顯然會根據模式而變化,這可以通過比較 \Styles\Themes\Light 看出。colour_name .xaml 和深色。colour_name .xaml 檔案,但我想通過實作我自己的 xaml<Color>資源以某種方式“擴展”這些資源,這些資源也會根據所選模式而改變。這可以做到嗎?
一個例子是當前使用藍色繪制點的圖表——這些在“淺色”模式下看起來不錯,但在“深色”模式下有點暗。我意識到我可以使用在兩種模式下看起來都不錯的更合適的藍色陰影,但是如果我可以定義一個顏色資源(例如)在淺色模式下為“藍色”但在深色模式下為“石灰”,它會更加靈活. 這種功能也可能有其他用途,例如彩色通知訊息。
uj5u.com熱心網友回復:
您可以根據內置主題創建自定義主題。創建一個資源字典,合并基礎主題的資源字典并覆寫現有畫筆并添加新畫筆。
輕主題詞典(Light.Blue.Custom.xaml):
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:options="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options" >
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Themes/Light.Blue.xaml" />
</ResourceDictionary.MergedDictionaries>
<!-- Overriding default brushes -->
<SolidColorBrush x:Key="MahApps.Brushes.Button.Border.MouseOver" Color="Blue" options:Freeze="True" />
<!-- Adding your own brushes -->
<SolidColorBrush x:Key="My.Brushes.Notification.Alert" Color="Red" options:Freeze="True" />
</ResourceDictionary>
黑暗主題詞典(Dark.Blue.Custom.xaml):
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:options="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options" >
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Themes/Dark.Blue.xaml" />
</ResourceDictionary.MergedDictionaries>
<!-- Overriding default brushes -->
<SolidColorBrush x:Key="MahApps.Brushes.Button.Border.MouseOver" Color="DarkBlue" options:Freeze="True" />
<!-- Adding your own brushes -->
<SolidColorBrush x:Key="My.Brushes.Notification.Alert" Color="DarkRed" options:Freeze="True" />
</ResourceDictionary>
在您的應用程式中注冊主題詞典。您必須在類和資源字典 URI中將虛擬名稱替換YourApp為應用程式的真實名稱。
public partial class YourApp : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
var lightBlueCustomTheme = ThemeManager.Current.AddLibraryTheme(new LibraryTheme(
new Uri("pack://application:,,,/YourApp;component/Light.Blue.Custom.xaml"),
MahAppsLibraryThemeProvider.DefaultInstance));
var darkBlueCustomTheme = ThemeManager.Current.AddLibraryTheme(new LibraryTheme(
new Uri("pack://application:,,,/YourApp;component/Dark.Blue.Custom.xaml"),
MahAppsLibraryThemeProvider.DefaultInstance));
ThemeManager.Current.ChangeTheme(this, lightBlueCustomTheme);
// Optionally enable App Mode theme switching
// ThemeManager.Current.ThemeSyncMode = ThemeSyncMode.SyncWithAppMode;
// ThemeManager.Current.SyncTheme();
}
}
Note that you need to provide themes for both Light and Dark, if you want to switch modes. For more information about themes and customization, you can refer to the documentation.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/448328.html
標籤:wpf mahapps.metro 主题化
