在 Windows Phone 中转换颜色 [英] Converting colour in Windows Phone

查看:20
本文介绍了在 Windows Phone 中转换颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Windows Phone 应用程序 中,我从 xml 获取颜色,然后将其绑定到某个元素.
我发现在我的情况下我得到了错误的颜色.

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.

这是我的代码:

 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));

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

So after converting the colour, I get the wrong result. In xml I have this one:

 <Color name="HeaderColor">#FFc50000</Color>

并转换为#FFFFC500

推荐答案

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

You should use some 3rd-party converter.

这是其中之一.

然后你可以这样使用它:

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);
}

这篇关于在 Windows Phone 中转换颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆