Windows Phone 7 - dynamically set button background color from Hex? [duplicate]

故事扮演 提交于 2020-01-02 02:22:28

问题


Possible Duplicate:
Change custom color for Rectangle.Fill or Grid.Background

I'm trying to dynamically set a button background color from Hex in Windows Phone 7.

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

ColorTranslator seems to not be available. That line gives a compiler error that it isn't found.

Am I looking in the wrong place (a different namespace?), or is there another way to do this from code?


回答1:


This class is not available in 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)
        )
    );
}



回答2:


This StackOverflow answer gives an even simpler way to use hex values to create a SolidColorBrush.

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


来源:https://stackoverflow.com/questions/4305968/windows-phone-7-dynamically-set-button-background-color-from-hex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!