2013-09-10 52 views
1

来解析HTML颜色名称到的SolidColorBrush在.NET中有一类蒙上了文本HTML颜色名称的颜色(在这种情况下,“红”):如何在Windows Store应用

Color col=(Color)ColorConverter.ConvertFromString("Red"); 
Brush brush=new SolidColorBrush(col); 

(这是我从这里拿了:Cast Color Name to SolidColorBrush

这适用于几乎all the colours that can be found on wikipedia

是否有Windows Store Apps,可以做同样的事情的等价类/库?

+0

我不知道* Windows的商店应用多*。这是不是可用'var color = System.Drawing.ColorTranslator.FromHtml(“red”);' – I4V

+0

不幸的是它不是......或者至少它不在同一个命名空间。 – JustinHui

+0

如果你经常这样做,你可能只想构建和缓存像这样的字典:http://stackoverflow.com/questions/12751008/how-to-enumerate-through-colors-in-winrt – WiredPrairie

回答

4

试试这个

using System.Reflection; 

public SolidColorBrush ColorStringToBrush(string name) 
{ 
    var property = typeof(Colors).GetRuntimeProperty(name); 
    if (property != null) 
    { 
     return new SolidColorBrush((Color)property.GetValue(null)); 
    } 
    else 
    { 
     return null; 
    } 
} 
+0

仅供参考 - WinRT中不存在'GetRuntimeProperty'。 – WiredPrairie

+0

[It exists](http://stackoverflow.com/a/13197768/1230188)。您需要在cs文件的顶部手动编写'using System.Reflection;'。 – Xyroid

+0

啊。这是一种扩展方法。 :) – WiredPrairie

相关问题