【解決方法】C# WPF で選択したカラー テーマを保存するために、マテリアル デザインのカラー テーマをテキスト ファイルに保存するにはどうすればよいですか?

プログラミングQA


マテリアル デザインを使用しています。 ユーザーが色とテーマを選択した後、ユーザーが望む色を選択するためのカラーピッカーを作成しました。

これらの設定をディスク上のテキスト ファイルに保存したいと考えています。 これらのタイプを、保存されているテーマを読み取るために使用できる文字列のリストに変換する方法がわかりません。

XAML:

XML
<Window x:Class="WpfApp5.SettingThemsWins.MaterialThemSettingy"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApp5.SettingThemsWins"
    mc:Ignorable="d"
    xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"

    Background="{DynamicResource MaterialDesignPaper}"

    Title="Setting" Height="607" Width="1144" WindowStartupLocation="CenterScreen">
<Grid>
    <materialDesign:ColorPicker x:Name="MyColorPicker1" HorizontalAlignment="Left" Margin="20,17,0,0" VerticalAlignment="Top" Height="353" Width="750" PreviewMouseMove="MyColorPicker1_PreviewMouseMove" />
    <ToggleButton x:Name="ThemeActivationsBtn"  Style="{StaticResource MaterialDesignSwitchToggleButton}"  ToolTip="Activation Of Dark Theme"  IsChecked="False" Margin="110,380,0,0" Click="ThemeActivationsBtn_Click" HorizontalAlignment="Left" Width="63" Height="27" VerticalAlignment="Top" />
    <Label Content="Dark Theme :" HorizontalAlignment="Left" Height="24" Margin="20,382,0,0" VerticalAlignment="Top" Width="85"/>
    <Button x:Name="SaverThemy" Content="Save Theme" HorizontalAlignment="Left" Margin="200,375,0,0" VerticalAlignment="Top" Width="170" Click="SaverThemy_Click"/>
</Grid>
</Window>

コードビハインド:

C#
namespace WpfApp5.SettingThemsWins
{
    /// <summary>
    /// Interaction logic for MaterialThemSettingy.xaml
    /// </summary>
    public partial class MaterialThemSettingy : Window
    {
        private readonly PaletteHelper _paletteHelper = new PaletteHelper();
        bool isDark;

        public MaterialThemSettingy()
        {
            InitializeComponent();
            //EmptySampleWind.Window1 window1 = new EmptySampleWind.Window1();
            //window1.Show();
        }

        public static IEnumerable<string> SortByLength(IEnumerable<string> e)
        {
            // Use LINQ to sort the array received and return a copy.
            var sorted = from s in e
                         orderby s.Length ascending
                         select s;
            return sorted;
        }
 
        private void MyColorPicker1_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            string filepath = @"C:\Themses";

            if (e.LeftButton == MouseButtonState.Pressed)
            {
                ITheme theme = _paletteHelper.GetTheme();

                theme.SetPrimaryColor(Color.FromRgb(MyColorPicker1.Color.R, MyColorPicker1.Color.G, MyColorPicker1.Color.B)); //red

                var Test = theme.GetBaseTheme();

                // something here to write all setting inside of ITheme into the text file

                //

                _paletteHelper.SetTheme(theme);
            }
        }

        private void ThemeActivationsBtn_Click(object sender, RoutedEventArgs e)
        {
            isDark = (bool)ThemeActivationsBtn.IsChecked;

            if (isDark)
            {
                ITheme theme = _paletteHelper.GetTheme();
                IBaseTheme baseTheme = isDark ? new MaterialDesignDarkTheme() : (IBaseTheme)new MaterialDesignLightTheme();
                theme.SetBaseTheme(baseTheme);

                _paletteHelper.SetTheme(theme);
            }
            else
            {
                ITheme theme = _paletteHelper.GetTheme();
                IBaseTheme baseTheme = isDark ? new MaterialDesignDarkTheme() : (IBaseTheme)new MaterialDesignLightTheme();
                theme.SetBaseTheme(baseTheme);

                _paletteHelper.SetTheme(theme);
            }
        }
    }
}

助けてください

私が試したこと:

C#
private void MyColorPicker1_PreviewMouseMove(object sender, MouseEventArgs e)
{
        string filepath = @"C:\Themses";

        if (e.LeftButton == MouseButtonState.Pressed)
        {
            ITheme theme = _paletteHelper.GetTheme();

            theme.SetPrimaryColor(Color.FromRgb(MyColorPicker1.Color.R, MyColorPicker1.Color.G, MyColorPicker1.Color.B)); //red

            var Test = theme.GetBaseTheme();

            // something here to write all setting inside of ITheme into the text file

            //

            _paletteHelper.SetTheme(theme);
        }
}

解決策 1

JSON にシリアル化してテキストとして書き込めば、テキストを読み取ってオブジェクトに逆シリアル化できます。

アップデート

簡単な例…

保存および取得するサンプル クラス:

C#
public class Theme
{
    public string ForeColor { get; set; }
    public string BackColor { get; set; }
}

1.JSONにシリアライズ、ファイルに保存

C#
Theme myTheme = new() { ForeColor = "red", BackColor = "white" };

string rawJson = System.Text.Json.JsonSerializer.Serialize(myTheme);
File.WriteAllText("Theme.json", rawJson);

2. ファイルからロードしてから逆シリアル化:

C#
string readRawJson = File.ReadAllText("Theme.json");

Theme readMyTheme = System.Text.Json.JsonSerializer.Deserialize<Theme>(readRawJson);

コメント

タイトルとURLをコピーしました