I'm trying to make a program to parse xml files with predefined format, and add some UI controls to my MainPage in Windows Universal Application.
In some part, I need to specify the background color of my TextBlocks in related xml file, so I'm looking for a way to convert string attribute, read from xml and convert it to Windows.UI.Color corresponding value.
here is my xml file and my C# code to add control
xml :
<test-unit name ="FOG_LAMP" text ="Fog Lamp" mode ="DIG_IN" color="ORANGE"/>
C#:
public void AddNewTextBlock(String Name, String Text, String Color)
{
TextBlock NewTextBlock = new TextBlock();
NewTextBlock.Name = Name;
NewTextBlock.Text = Text;
NewTextBlock.FontSize = 24;
myGrid.Children.Add(NewTextBlock);
}
Thanks For Help
You can use XamlBindingHelper
to convert the string
value to Color
-
var color = (Color)XamlBindingHelper.ConvertValue(typeof(Color), "ORANGE");
var brush = new SolidColorBrush(color);
NewTextBlock.Foreground = brush;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With