question

MojtabaHakim-8125 avatar image
0 Votes"
MojtabaHakim-8125 asked HuiLiu-MSFT answered

How do I save Theme of Material Design in a text file for saving color theme chosen in C# WPF?

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 :


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

How can I do that?

Full XAML:


  <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:

 using MaterialDesignThemes.Wpf;
 using System;
 using System.Collections.Generic;
 using System.IO;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows;
 using System.Windows.Controls;
 using System.Windows.Data;
 using System.Windows.Documents;
 using System.Windows.Input;
 using System.Windows.Media;
 using System.Windows.Media.Imaging;
 using System.Windows.Shapes;
    
 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);
             }
         }
     }
 }

How can I do that ?






dotnet-csharpwindows-wpfdotnet-wpf-xaml
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

HuiLiu-MSFT avatar image
1 Vote"
HuiLiu-MSFT answered

Hi,@MojtabaHakim-8125. You could try to refer to the code below and combine it to get the selected color of ColorPicker.

I use the following code to transfer the color data to a txt file in json format. But I'm not familiar with Material Design's ColorPicker, and I don't know how to get the selected color.
Or could you share how to get the ColorPicker's selected color?

262480-materialdesign.txt

The result:
262500-image.png


If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



materialdesign.txt (3.6 KiB)
image.png (3.3 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.