2

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

したがって、色を変換した後、間違った結果が得られます。xmlで私はこれを持っています:

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

そしてそれはに変換します#FFFFC500

4

1 に答える 1

12

サードパーティのコンバーターを使用する必要があります。

ここにそれらの1つがあります

次に、次のように使用できます。

Color color = (Color)(new HexColor(resources.GetValue());

また、このリンクからメソッドを使用することもできます。同様に機能します。

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);
}
于 2012-07-31T11:53:29.403 に答える