Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Xamarin.Forms.Color to platform specific color

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?

like image 490
bkardol Avatar asked Aug 07 '14 06:08

bkardol


3 Answers

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);
like image 137
Stephane Delcroix Avatar answered Oct 07 '22 16:10

Stephane Delcroix


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();
like image 40
SanderE Avatar answered Oct 07 '22 16:10

SanderE


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);
like image 3
Zac Avatar answered Oct 07 '22 17:10

Zac