原文: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里面有的颜色。否则会报错。

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

  1. springMvc把client传过来一个String类型,转换为日期类型为例

    springMvc--接受日期类型参数处理   目录 步骤 2.自定义类型转换规则 3.注册自定义的类型转换类 4.地址栏访问 这个问题,也即是springMvc如何进行参数类型的转换 , 以把cli ...

  2. Java中String类型细节

    Java中String类型细节 一 . String两种初始化方式 1 . String str1= “abc”;//String类特有的创建字符对象的方式,更高效 在字符串缓冲区中检测”abc”是否 ...

  3. WPF System.InvalidCastException: 无法将类型为“System.Windows.Media.Color”的对象强制转换为类型“System.Windows.Media.Brush”。

    场景:添加ComboBox样式,界面卡死,日志异常文件如下: -- ::, | ERROR | System.InvalidCastException: 无法将类型为“System.Windows.M ...

  4. Java中String类型详解

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

  5. Redis 中 String 类型的内存开销比较大

    使用 String 类型内存开销大 1.简单动态字符串 2.RedisObject 3.全局哈希表 使用 Hash 来存储 总结 参考 使用 String 类型内存开销大 如果我们有大量的数据需要来保 ...

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

    代码 // string类型转基本数据类型 package main import ( "fmt" "strconv" ) func main() { str1 ...

  7. Delphi中String类型原理介绍

    Delphi中字符串的操作很简单,但幕后情况却相当复杂.Pascal传统的字符串操作方法与Windows不同,Windows吸取了C语言的字符串操作方法.32位Delphi中增加了长字符串类型,该类型 ...

  8. C#中string类型是值类型还是引用类型?

    .Net框架程序设计(修订版)中有这样一段描述:String类型直接继承自Object,这使得它成为一个引用类型,也就是说线程上的堆栈上不会驻留有任何字符串. string类型(引用类型) 名称 CT ...

  9. 【转载】 Java中String类型的两种创建方式

    本文转载自 https://www.cnblogs.com/fguozhu/articles/2661055.html Java中String是一个特殊的包装类数据有两种创建形式: String s ...

随机推荐

  1. 6.Swift教程翻译系列——Swift集合类型

    英文版PDF下载地址http://download.csdn.net/detail/tsingheng/7480427 Swift提供数组和字典两种集合类型.用来存储很多值的情况.数组有序的存储一组同 ...

  2. C++ 快速入门笔记:基本语法

    数据类型 枚举类型 enum color { red, green, blue } colors; colors = blue; 默认情况下,第一个名称的值是 0,后面的依次加 1.也可以自定初始值: ...

  3. [Angular] @ViewChild and template #refs to get Element Ref

    We can use @ViewChild with component: @ViewChild(AuthMessageComponent) message: AuthMessageComponent ...

  4. Oracle中的游标(转)

    Oracle中的游标有两种:显式游标.隐式游标.显示游标是用cursor...is命令定义的游标,它可以对查询语句(select)返回的多条记录进行处理,而隐式游标是在执行插入 (insert).删除 ...

  5. VB的MSHFlexGrid控件内容导入Excel

    机房收费系统中有非常多窗口用到导出到Excel,说一下vb与Excel的交互,怎样才干将MSHFlexgrid中的内容导出到Excel. 首先在VB中加入引用Microsoft Excel 14.0 ...

  6. iOS项目double、float精度丢失解决办法

    描述 在iOS项目中老是遇到double.float精度丢失的问题 PS: NSString * jsonStr = @"{\"9.70\":9.70,\"67 ...

  7. eCognition学习记录

    作者:朱金灿 来源:http://blog.csdn.net/clever101 昨天公司从外面请了人讲解eCognition的最新进展及项目二次开发应用情况.我做了大致下面记录: 1.  eCogn ...

  8. Java网络编程注意事项1

    网络编程的基础知识 什么是计算机网络,就是把分布在不同地理区域的计算机与专门的外部设备通信线路互连成一个规模大.功能强的网络系统. 计算机网络主要能做些下面功能: 1)资源共享 2)信息传输与集中处理 ...

  9. IP packet transmission using vehicular transport

    In one embodiment, a first stationary router may detect a disconnected backhaul link to a destinatio ...

  10. 常见Python 中pip用法(待继续添加)

    常见pip用法 * pip install numpy             --安装包numpy * pip uninstall numpy           --卸载包numpy * pip ...