0

I implemented a theme system for my program, and I read the theme from a file at startup.

Basically in my App.xaml I have a bunch of <Colors/> with specific keys which I set the value of in code-behind when starting the program in App.xaml.cs.Main();

Here's what it looks like:

public static void Main()
{
   var application = new App();

   application.InitializeComponent();

   LoadTheme();

   application.Run();
}

And the LoadTheme-function looks like this:

public static void LoadTheme()
{
   UItheme theme = UItheme.FromFile(themePath);

   Color AccentColor = (Color)App.Current.FindResource("AccentColor");

   Color PrimaryColor = (Color)App.Current.FindResource("PrimaryColor");
   Color PrimaryLightColor = (Color)App.Current.FindResource("PrimaryLightColor");
   Color PrimaryDarkColor = (Color)App.Current.FindResource("PrimaryDarkColor");

   Color PrimaryTextColor = (Color)App.Current.FindResource("PrimaryTextColor");
   Color SecondaryTextColor = (Color)App.Current.FindResource("SecondaryTextColor");
   Color IconColor = (Color)App.Current.FindResource("IconColor");
   Color BorderColor = (Color)App.Current.FindResource("BorderColor");



   AccentColor = theme.AccentColor;
   PrimaryColor = theme.PrimaryColor;
   PrimaryLightColor = theme.PrimaryLightColor;
   PrimaryDarkColor = theme.PrimaryDarkColor;
   PrimaryTextColor = theme.PrimaryTextColor;
   SecondaryTextColor = theme.SecondaryTextColor;
   IconColor = theme.IconColor;
   BorderColor = theme.BorderColor;

   Console.WriteLine(((Color)App.Current.FindResource("AccentColor")).ToString());

}

Maybe not the prettiest function but I thought it would get the job done.

What seems to be my problem is that when I set these (what should be references to the Color-resource), the value of the resource itself doesn't seem to change. Just as if they were readonly.

The last line always prints out the following (from App.xaml):

<Color x:Key="AccentColor" A="255" R="123" G="123" G="123"/> // aka the values I declared the resource with in XAML.

even though my theme has different colors.

I must be doing something wrong here, but I don't know what. Any help would be great.

1 Answer 1

1

Color is a structure, which means it is passed by value, not by reference. You are basically making copies of the colors, modifying those copies, and then letting them go out of scope and get deleted. You should put the colors into the resource dictionary by doing something along the lines of Application.Current.Resources["key"] = value.

Sign up to request clarification or add additional context in comments.

1 Comment

Wow. I did not see that Color was a struct. Well, thank you very much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.