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 :-

[ContentProperty("ColorAsHsla")]
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.

Update

After reading page 146 of the following :-

https://download.xamarin.com/developer/xamarin-forms-book/BookPreview2-Ch08-Rel0203.pdf

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


    
       
          
             0.67
             1.0
             0.5
             1.0
          
       
    

No comments:

Post a Comment