0

I'm trying to give user a possibility to change color of the application. I'm using WPF Extended Toolkit to get Color and then save it into Settings so when I run again it, color will be applied. The problem is that for Rectangle it works perfectly but for Label and it background, when I run application again it turns automaticly white instead to saved Color. Here's my code:

Options class and 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 where I have label(TopBarMain) and rectangle(Background)

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");
        }


    }

And I call it like this then running an application:

public MainWindow()
    {
        InitializeComponent();

        ColorChange(Settings.Default.Main_Background, Settings.Default.Main_TopBack);

        option = new Options(this);

    }

Where option is:

Options option;

To call Options class. I have no clue anymore where is the problem. Please help.

4

1 に答える 1

0

わかりました。問題は、コンポーネントが初期化されたときに、カラーキャンバスの選択された色を変更していたときに、選択された色イベントが自動的に実行され、私の設定の 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();
        }

    }

しかし、助けてくれてありがとう。

于 2013-09-27T14:33:10.363 に答える