Windows Store Application - XAML - C # - Access ThemeResource in Code

I have a Styles.xaml file where I declared different colors and styles.

    <SolidColorBrush x:Key="MyColour" Color="#FFFF411E"/>

I want to access this in code, since I have control, and I want to bind the background color to the object in the object. This is easy to do, however, an object is created from a response to a server call, and the property can be one of many colors. I could define a color in the code, but since I use the same color for the other controls defined in XAML, I don’t want the same color to point twice in the application. Therefore, I want to be able to access the brush SolidColorBrushin the code in order to use it.

Any idea how to do this?

+4
source share
2 answers

Try it.

 Application.Current.Resources[ "MyColour" ] as SolidColorBrush 
+5
source

you must first have a ResourceDictionary in your xaml or file.cs.

xaml: <ResourceDictionary Source="Styles.xaml"/>

code: Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri(@"/" + this.GetType().Assembly.GetName().Name + ";component/" + @"Styles.xaml", UriKind.Relative) });

then you can access it like this

SolidColorBrush mycolor = Resources["MyColour"] as SolidColorBrush;
0
source

Source: https://habr.com/ru/post/1544144/


All Articles