Reputation: 1959
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.
Upvotes: 0
Views: 1132
Reputation: 3404
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
.
Upvotes: 1