我想实现更改程序的主题并保存它。当您退出程序时,将加载保存的主题。当您选择主题时,主题会发生变化,但不会保存(需要修复什么?
项目浏览器
应用程序.xaml
<Application x:Class="ConvertToImage.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ConvertToImage"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles\DarkTheme.xaml"/>
<ResourceDictionary Source="Styles\LightTheme.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
应用程序.xaml.cs
using System.Windows;
using ConvertToImage.Properties;
namespace ConvertToImage
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
private void Application_Startup(object sender, StartupEventArgs e)
{
string savedTheme = Settings.Default.ThemeName;
ApplyTheme(savedTheme);
}
public static void ApplyTheme(string theme)
{
ResourceDictionary resourceDict = new ();
switch (theme)
{
case "Dark":
resourceDict.Source = new Uri("/Styles/DarkTheme.xaml", UriKind.Relative);
break;
case "Light":
resourceDict.Source = new Uri("/Styles/LightTheme.xaml", UriKind.Relative);
break;
}
Application.Current.Resources.MergedDictionaries.Clear();
Application.Current.Resources.MergedDictionaries.Add(resourceDict);
}
}
}
文件设置.Designer.cs
//------------------------------------------------------------------------------
// <auto-generated>
// Этот код создан программой.
// Исполняемая версия:4.0.30319.42000
//
// Изменения в этом файле могут привести к неправильной работе и будут потеряны в случае
// повторной генерации кода.
// </auto-generated>
//------------------------------------------------------------------------------
namespace ConvertToImage.Properties {
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.10.0.0")]
public partial class Settings : global::System.Configuration.ApplicationSettingsBase {
public static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
public static Settings Default {
get {
return defaultInstance;
}
}
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("")]
public string ThemeName {
get {
return ((string)(this["ThemeName"]));
}
set {
this["ThemeName"] = value;
}
}
}
}
带按钮的页面
<Grid Style="{DynamicResource GridBackgroundBrush}">
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<Button Width="200" Height="30" Content="Светлый"
x:Name="LinghtBtn" Click="Test_Click" />
<Button Width="200" x:Name="DartBtn" Content="Тёмный"
Height="30" Click="Test_Click" />
</StackPanel>
</Grid>
切换主题
private void Test_Click(object sender, RoutedEventArgs e)
{
Button clickedButton = sender as Button;
if (clickedButton != null)
{
string selectedTheme = clickedButton.Name == "LinghtBtn" ? "Light" : "Dark";
Properties.Settings.Default.ThemeName = selectedTheme;
Properties.Settings.Default.Save();
App.ApplyTheme(selectedTheme);
}
}
我期望主题被更改并保存,但主题没有保存。

更改了app.xaml