在 Windows 通用应用程序中将字符串类型转换为 Windows.UI.Color [英] Converting String Type To Windows.UI.Color In Windows Universal App
问题描述
我正在尝试编写一个程序来解析具有预定义格式的 xml 文件,并向我的 Windows 通用应用程序的 MainPage 添加一些 UI 控件.
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.
在某些方面,我需要在相关的xml文件中指定我的TextBlocks的背景颜色,所以我正在寻找一种方法来转换字符串属性,从xml中读取并将其转换为Windows.UI.Color对应的值.
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.
这是我的 xml 文件和我的 C# 代码来添加控件
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);
}
感谢帮助
推荐答案
您可以使用 XamlBindingHelper
将 string
值转换为 Color
-
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;
这篇关于在 Windows 通用应用程序中将字符串类型转换为 Windows.UI.Color的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!