I'm looking for a way to convert a Xamarin.Forms.Color to a platform specific color. For example the Android.Graphics.Color for Android.
I took a look at the properties of the Xamarin.Forms.Color like R, G & B. The values only contain a 0 or 1 so that seems to be pretty worthless. Has someone experienced and solved this issue before?
I guess you try to do this in a custom renderer.
In iOS, you'd do:
UIColor uicolor = yourXFColor.ToUIColor ();
In Android:
Android.Graphics.Color adColor = yourXFColor.ToAndroidColor ();
Unfortunately, the equivalent extension methods are not public for WP, but you can do this:
System.Windows.Media.Color wpColor = System.Windows.Media.Color.FromArgb (
(byte)(yourXFColor.A * 255),
(byte)(yourXFColor.R * 255),
(byte)(yourXFColor.G * 255),
(byte)(yourXFColor.B * 255));
then eventually:
Brush brush = new SolidColorBrush (wpColor);
Currently you can do this with the "ToAndroid()" extension method in Xamarin.Forms.Platform.Android.
using Xamarin.Forms.Platform.Android;
Android.Graphics.Color droidColor = formsColor.ToAndroid();
Going off of the previous answers here, but Xamarin has now placed the ToAndroid() method in a ColorExtensions helper class.
using Xamarin.Forms.Platform.Android
....
Android.Graphics.Color color = ColorExtensions.ToAndroid(formsColor);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With