在Xamarin.Forms中作为网格背景的渐变

在Xamarin.Forms中作为网格背景的渐变,xamarin.forms,custom-renderer,Xamarin.forms,Custom Renderer,我正在尝试使用渐变笔刷作为网格上的背景。到目前为止,我只为UWP创建了一个自定义渲染器,但我无法让它工作 e.NewElement.BackgroundColor需要一种颜色,但我有一个LinearGradientBrush。那么,甚至可以将网格背景设置为渐变色吗 谢谢 我的渲染器代码如下: public class MyGridRenderer:ViewRenderer<MyGrid, Grid> { protected override void OnElemen

我正在尝试使用渐变笔刷作为网格上的背景。到目前为止,我只为UWP创建了一个自定义渲染器,但我无法让它工作

e.NewElement.BackgroundColor需要一种颜色,但我有一个LinearGradientBrush。那么,甚至可以将网格背景设置为渐变色吗

谢谢

我的渲染器代码如下:

    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;
        }
    }
}
公共类MyGridRenderer:ViewRenderer { 受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e) { 基础。一个要素发生变化(e); if(例如NewElement!=null) { LinearGradientBrush=新的LinearGradientBrush(); GradientStop start=新的GradientStop(); 起始偏移量=0; start.Color=Colors.Yellow; brush.GradientStops.Add(开始); GradientStop=新的GradientStop(); 停止。偏移量=1; stop.Color=Colors.Black; brush.GradientStops.Add(停止); e、 NewElement.BackgroundColor=brush;//这里是什么 //Control.Background=画笔; } } } 我有答案

有一个背景属性

因此:


如何获得LinearGradientBrush?请您扩展您的示例,以显示using语句,并对所需的任何引用进行注释。
Background=brush