2010-11-29 70 views
8

可能重複:
Change custom color for Rectangle.Fill or Grid.BackgroundWindows Phone 7 - 從十六進制動態設置按鈕背景顏色?

我想動態設置從十六進制按鈕的背景顏色在Windows Phone 7的

SolidColorBrush myBrush = new SolidColorBrush(); 
    myBrush.Color = ColorTranslator.FromHtml("#123456"); 
    pbMood.Background = myBrush; 

ColorTranslator似乎不可用。該行給出了一個編譯器錯誤,它沒有找到。

我在錯誤的地方尋找(不同的命名空間?),還是有另一種方法從代碼做到這一點?

+0

感謝您提出這個問題。我很快就會需要這些信息。 – Cyberherbalist 2010-11-30 00:14:33

回答

23

該類在Silverlight中不可用。

Instead, you can write it yourself

public static SolidColorBrush GetColorFromHexa(string hexaColor) 
{ 
    return new SolidColorBrush(
     Color.FromArgb(
      Convert.ToByte(hexaColor.Substring(1, 2), 16), 
      Convert.ToByte(hexaColor.Substring(3, 2), 16), 
      Convert.ToByte(hexaColor.Substring(5, 2), 16), 
      Convert.ToByte(hexaColor.Substring(7, 2), 16) 
     ) 
    ); 
} 
+0

真棒回答。完善! – pearcewg 2010-11-29 17:07:33

+0

U Rock !!!!!!!! – Cyberherbalist 2010-11-30 00:14:52

3

StackOverflow answer給使用十六進制值創建的SolidColorBrush一個更簡單的方法。

Brush brush = new SolidColorBrush(new Color() { R = 0xFF, G = 0xA6, B = 0x10}); 
相關問題