Wednesday, 4 March 2015

Setting Color as HSLA using XAML Extension for Xamarin.Forms

The following shows how to set a Color to a HSLA value using a XAML Extension.

Firstly, define a new class called ColorAsHslaExtension.cs :-

public class ColorAsHslaExtension : IMarkupExtension
 public string ColorAsHsla { get; set; }

 public object ProvideValue(IServiceProvider serviceProvider)
  var elements = ColorAsHsla.Split(',');

  double h = double.Parse(elements[0]);
  double s = double.Parse(elements[1]);
  double l = double.Parse(elements[2]);
  double a = double.Parse(elements[3]);

  return Color.FromHsla(h, s, l, a);

Next, define the XAML that you make use of the Extension :-


Hope this helps.


After reading page 146 of the following :-

It seems there is a much simpler way of achieving this :-