在Windows Phone中转换颜色

rev*_*kpi 2 c# silverlight silverlight-4.0 windows-phone-7

在我的Windows Phone application我从xml获取颜色,然后将其绑定到一些元素.
我发现在我的情况下我得到了错误的颜色.

这是我的代码:

 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));
Run Code Online (Sandbox Code Playgroud)

因此在转换颜色后,我得到了错误的结果.在xml中我有这个:

 <Color name="HeaderColor">#FFc50000</Color>
Run Code Online (Sandbox Code Playgroud)

它转化为 #FFFFC500

vor*_*olf 12

你应该使用一些第三方转换器.

这是其中之一.

然后你可以使用它:

Color color = (Color)(new HexColor(resources.GetValue());
Run Code Online (Sandbox Code Playgroud)

您也可以使用此链接中的方法,它也可以.

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);
}
Run Code Online (Sandbox Code Playgroud)