2013-09-10 58 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

相關問題