When assigning a value to a background, other colors can be given by the following methods, in addition to their own red,blue,black.
The main is to convert hex to ARGB (A:Alpha, which represents transparency, r:red, G:green, B:blue), where ARGB values are between 0--255
The string parameter passed in by this method can be similar to #21459A or #FF21459A
PublicStatic Color?Getcolorfromhex (String colorstr)//? Indicates that the return value can be null{if (colorstr! =Null && (Colorstr.length = =7 | | Colorstr.length = =9)) {byte A =255;int posi =1;if (colorstr.length = =9) {a = Byte.parse (Colorstr.substring (Posi,2), Numberstyles.hexnumber); Posi + =2; }byte R = Byte.parse (colorstr.substring (Posi, 2), numberstyles.hexnumber); Posi + = 2; byte g = Byte.parse (Colorstr.substring (Posi, 2), numberstyles.hexnumber); Posi + = 2; byte B = Byte.parse (Colorstr.substring (Posi, 2), numberstyles.hexnumber); return Color.FromArgb (A, R, G, b);} return null;}
Then assign a value to the background color
Grid New SolidColorBrush (Getcolorfromhex ("#21459A"). Value);
Of course, when you know the RGB can also take the following ways:
Grid New SolidColorBrush () {Color = Color.FromArgb (255,154)}; // This . Foreground = new SolidColorBrush (Color.FromArgb (255,33,, 154)); // Object not created this way.
PS: During the programming process, copy the color value string from Skype on Win8.1. The length should be more than normal.
(The reason is that at the end there will be something for "" ...) It's really empty, but with the keyboard arrow keys you can see that there's something inside.
and is able to extract this empty character. But also because of this little detail troubled for a long time.
Convert hex color value to color