问题
In my Windows Phone application
I get the colour from xml and then bind it to some element.
I have found that I get the wrong colour in my case.
Here is my code:
var resources = feedsModule.getResources().getColorResource("HeaderColor") ??
FeedHandler.GetInstance().MainApp.getResources().getColorResource("HeaderColor");
if (resources != null)
{
var colourText = Color.FromArgb(255,Convert.ToByte(resources.getValue().Substring(1, 2), 16),
Convert.ToByte(resources.getValue().Substring(3, 2), 16),
Convert.ToByte(resources.getValue().Substring(5, 2), 16));
So after converting the colour, I get the wrong result. In xml I have this one:
<Color name="HeaderColor">#FFc50000</Color>
and it converts into #FFFFC500
回答1:
You should use some 3rd-party converter.
Here is one of them.
Then you can use it so:
Color color = (Color)(new HexColor(resources.GetValue());
Also you can use the method from this link, it works as well.
public Color ConvertStringToColor(String hex)
{
//remove the # at the front
hex = hex.Replace("#", "");
byte a = 255;
byte r = 255;
byte g = 255;
byte b = 255;
int start = 0;
//handle ARGB strings (8 characters long)
if (hex.Length == 8)
{
a = byte.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
start = 2;
}
//convert RGB characters to bytes
r = byte.Parse(hex.Substring(start, 2), System.Globalization.NumberStyles.HexNumber);
g = byte.Parse(hex.Substring(start + 2, 2), System.Globalization.NumberStyles.HexNumber);
b = byte.Parse(hex.Substring(start + 4, 2), System.Globalization.NumberStyles.HexNumber);
return Color.FromArgb(a, r, g, b);
}
来源:https://stackoverflow.com/questions/11737583/converting-colour-in-windows-phone