Search code examples
xamarin.formscustom-renderer

Gradient as background on a grid in Xamarin.Forms


I'm trying to use a gradient brush for a background on a Grid. So far I've created a custom renderer for the UWP only but I can't get that to work.

The e.NewElement.BackgroundColor expects a Color, but I have a LinearGradientBrush. So is it even possible to set the grid background as a gradient color?

Thanks

My renderer code is below:

    public class MyGridRenderer:ViewRenderer<MyGrid, Grid>
{
    protected override void OnElementChanged(ElementChangedEventArgs<MyGrid> e)
    {
        base.OnElementChanged(e);

        if (e.NewElement != null)
        {
            LinearGradientBrush brush = new LinearGradientBrush();

            GradientStop start = new GradientStop();
            start.Offset = 0;
            start.Color = Colors.Yellow;
            brush.GradientStops.Add(start);

            GradientStop stop = new GradientStop();
            stop.Offset = 1;
            stop.Color = Colors.Black;
            brush.GradientStops.Add(stop);

            e.NewElement.BackgroundColor = brush; //What goes here
            //Control.Background = brush;
        }
    }
}

Solution

  • I have the answer.

    There is a background property.

    so:

    Background=brush