UWP中String类型如何转换为Windows.UI.Color

我在学习过程中遇到的,我保存主题色为string,但在我想让StatusBar随着主题色变化时发现没法使用。

1 ThemeColorHelper tc = new ThemeColorHelper();
2 StatusBar statusbar = StatusBar.GetForCurrentView();
3 statusbar.BackgroundColor = (Color)tc.ThemeColor;
4 statusbar.BackgroundOpacity = 1;
5 statusbar.ForegroundColor = Colors.Black;

这样一运行就会报错,

苦恼了很久都没有解决,最后求助大神说通过创建Dictionary来解决。

 1  if (ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons"))
 2             {
 3                 Dictionary<string, Color> color = new Dictionary<string, Color>()
 4                 {
 5                     ["SkyBlue"] = Colors.SkyBlue,
 6                     ["Pink"] = Colors.Pink,
 7                     ["LightGreen"] = Colors.LightGreen
 8                 };
 9                 ThemeColorHelper tc = new ThemeColorHelper();
10                 StatusBar statusbar = StatusBar.GetForCurrentView();
11                 statusbar.BackgroundColor = color[tc.ThemeColor.ToString()];
12                 statusbar.BackgroundOpacity = 1;
13                 statusbar.ForegroundColor = Colors.Black;
14             }

这样有点麻烦,所以大神有说了另一种方法。

 if(ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons"))
            {
                ThemeColorHelper tc = new ThemeColorHelper();
                Color color = (Color)typeof(Colors).GetProperty(tc.ThemeColor.ToString()).GetValue(this);
                StatusBar statusbar = StatusBar.GetForCurrentView();
                statusbar.BackgroundColor = color;
                statusbar.BackgroundOpacity = 1;
                statusbar.ForegroundColor = Colors.Black;
            }

完美解决。但需要注意这个需要引用 using System.Reflection;并且必须使用colors里面有的颜色。否则会报错。

时间: 2024-08-21 14:23:22

UWP中String类型如何转换为Windows.UI.Color的相关文章

Java中String类型详解

这篇博客是我一直想总结的,这两天一直比较忙,先上传下照片吧,过后有时间再弄成正常的. 本文主要是对Java中String类型的总结,包括其在JVM中是怎么存储的...

C++中string类型对象和double型变量之间的互相转换

//convert string type value to double type value string s = "23"; double d; istringstream is(s); is>>d; cout<<d<<endl;   //输出23 //convert double type value to string type value double d=45; string s; ostringstream os; os<<

java中String类型转换方法

integer to String : int i = 42;String str = Integer.toString(i);orString str = "" + idouble to String :String str = Double.toString(i);long to String :String str = Long.toString(l);float to String :String str = Float.toString(f);String to intege

(转)Java中String类型的参数传递问题

这篇文章主要介绍了简单谈谈Java中String类型的参数传递问题的相关资料,需要的朋友可以参考下 提要:本文从实现原理的角度上阐述和剖析了:在Java语言中,以 String 作为类型的变量在作为方法参数时所表现出的“非对象”的特性. 一.最开始的示例 写代码最重要的就是实践,不经过反复试验而得出的说辞只能说是凭空遐想罢了.所以,在本文中首先以一个简单示例来抛出核心话题: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 public clas

Java中String类型细节

Java中String类型细节 一 . String两种初始化方式 1 . String str1= "abc";//String类特有的创建字符对象的方式,更高效 在字符串缓冲区中检测"abc"是否存在 若存在则不重复创建,将地址赋值给str1. 若不存在,则在字符串缓冲区中创建对象并赋地址给str1. 2 . String str1= new String( "abc"); //构造函数初始化  或者  char [] ch={'a','b'

go中string类型转换为基本数据类型的方法

代码 // string类型转基本数据类型 package main import ( "fmt" "strconv" ) func main() { str1 := "false" var b bool // 函数ParseBool会返回两个值 // "_"表示会忽略掉第二个返回值 b, _ = strconv.ParseBool(str1) fmt.Printf("str1原值为%q, 转换为%T类型, 值为%v

Java中String类型的参数传递问题的解析

一.引入示例 public class StringAsParamOfMethodDemo { public static void main(String[] args) { StringAsParamOfMethodDemo sapm = new StringAsParamOfMethodDemo(); sapm.testA(); } private void testA() { String originalStr = "original"; System.out.println

java中String类型

String类型是字符串类型.. 字符串一旦创建不可以在改变."abc"字符串对象一旦创建,不可以再改成"abcd" 提升字符串的访问效率:在程序中使用了"缓存"技术.所以在java中所有使用"双引号"括起来的字符串都会在"字符串常量池"中创建一份.字符串常量池在方法区中被存储. 在程序执行过程中,如果用到某个字符串,例如:"abc"那么程序救护在字符串常量池中去搜索该字符串,如果没有找

C#中string类型的不可变性

string是C#中比较常用的字符串类型,之前一直都只是使用它,却没有深入的去了解,今天在书中忽然看到它具有不可变性:于是便仔细了解了一下. //声明并初始化string类型变量 string Text = “Hello”; //对string类型变量赋值 Text = “Hi” 在这里,在把Hi字符串赋给string变量Text之前 内存首先会重新初始化一块区域,并把区域的值初始化为Hi.之后,这块内存的地址将赋给变量Text,而原来存放hello的内存区域则不会改变,可以看出,string类