Click here to Skip to main content
15,566,185 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm using Material design. I created a color picker to choose the color the user wants, after the user chooses the color and theme.

I want to save these settings into a text file on the disk. I don't know how can I convert these types to a list for the string which can I use for reading theme that is saved:

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>



Code Behind:

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


please help

What I have tried:

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);
        }
}
Posted
Updated 20-Nov-22 0:09am
v2

1 solution

Why not serialize to JSON and write as text, then you can read the text and deserialize it to an object.

UPDATE

Simple example...

sample class to be stored and retrieved:
C#
public class Theme
{
    public string ForeColor { get; set; }
    public string BackColor { get; set; }
}

1. serializing to JSON, saving to file
C#
Theme myTheme = new() { ForeColor = "red", BackColor = "white" };

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

2. loading from file, and then deserializing:
C#
string readRawJson = File.ReadAllText("Theme.json");

Theme readMyTheme = System.Text.Json.JsonSerializer.Deserialize<Theme>(readRawJson);
 
Share this answer
 
v2
Comments
mojtabahakimian 19-Nov-22 23:44pm    
Yes, my problem is that I don't know how to convert this type of itheme into a list of strings or Jason, because this itheme cannot be used in a loop.
Graeme_Grant 19-Nov-22 23:51pm    
ITheme theme = _paletteHelper.GetTheme();

Returns a concrete class. Use the concrete implementation for serialization.
mojtabahakimian 19-Nov-22 23:56pm    
thanks for taking your to help me but May I ask you to write me a code for this ?
Graeme_Grant 20-Nov-22 0:02am    
I wrote an example for you to write your own implementation. It uses System.Text.Json (Dot Net 3.1+). If you're using Framework 4.8 or older, then use Newtonsoft.Json. Works basically the same.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900