Windows Phone 7 - 从十六进制动态设置按钮背景颜色? [英] Windows Phone 7 - dynamically set button background color from Hex?

查看:23
本文介绍了Windows Phone 7 - 从十六进制动态设置按钮背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
​​更改 Rectangle.Fill 或 Grid.Background 的自定义颜色

我正在尝试从 Windows Phone 7 中的十六进制动态设置按钮背景颜色.

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 似乎不可用.该行给出了未找到的编译器错误.

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?

推荐答案

该类在 Silverlight 中不可用.

This class is not available in Silverlight.

相反,您可以自己写.

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

这篇关于Windows Phone 7 - 从十六进制动态设置按钮背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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