我试图让用户改变应用程序的颜色。我正在使用WPF扩展工具包来获取Color然后将其保存到Settings中,所以当我再次运行它时,将应用颜色。问题是,对于Rectangle,它工作得很好但是对于Label和它的背景,当我再次运行应用程序时,它会自动转为白色而不是保存的颜色。这是我的代码:
选项类和xaml
public Options(MainWindow main)
{
InitializeComponent();
window = main;
BackgroundColor.SelectedColor = (Color)ColorConverter.ConvertFromString(Settings.Default.Main_Background);
TopBarBackColor.SelectedColor = (Color)ColorConverter.ConvertFromString(Settings.Default.Main_TopBack);
}
private void Color_SelectedColorChanged(object sender, RoutedPropertyChangedEventArgs<Color> e)
{
Settings.Default.Main_Background = BackgroundColor.HexadecimalString;
Settings.Default.Main_TopBack = TopBarBackColor.HexadecimalString;
Settings.Default.Save();
window.ColorChange(Settings.Default.Main_Background, Settings.Default.Main_TopBack);
}
MainWindow我有标签(TopBarMain)和矩形(背景)
public void ColorChange(String _Main_B, String _Main_TopBar)
{
var converter = new BrushConverter();
try
{
Background.Fill = (Brush)converter.ConvertFromString(_Main_B);
TopBarMain.Background = (Brush)converter.ConvertFromString(_Main_TopBar);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
Background.Fill = (Brush)converter.ConvertFromString("#E5EEEEEE");
TopBarMain.Background = (Brush)converter.ConvertFromString("#7FC3C3C3");
}
}
我称之为运行应用程序:
public MainWindow()
{
InitializeComponent();
ColorChange(Settings.Default.Main_Background, Settings.Default.Main_TopBack);
option = new Options(this);
}
选项是:
选项选项;
调用选项类。 我不知道问题出在哪里了。请帮忙。
答案 0 :(得分:0)
好的,我做到了。问题是当初始化Component时,我正在更改Color Canvas的Color Color,然后Selected Color事件自动运行,它将我的Settings Main_TopBack变量更改为默认值(白色)并保存。我只是实现了布尔控件,如果颜色加载如下:
bool ColorsLoaded = false;
public Options(MainWindow main)
{
InitializeComponent();
window = main;
BackgroundColor.SelectedColor = (Color)ColorConverter.ConvertFromString(Settings.Default.Main_Background);
TopBarBackColor.SelectedColor = (Color)ColorConverter.ConvertFromString(Settings.Default.Main_TopBack);
ColorsLoaded = true;
}
private void Color_SelectedColorChanged(object sender, RoutedPropertyChangedEventArgs<Color> e)
{
if (ColorsLoaded)
{
Settings.Default.Main_Background = BackgroundColor.HexadecimalString;
Settings.Default.Main_TopBack = TopBarBackColor.HexadecimalString;
Settings.Default.Save();
window.ColorChange();
}
}
但是感谢您的帮助。