0

There already is a solution on how to create a layout with a gradient color background, but when I'm trying to implement it, it tells me that the Android renderer is obsolete and that I should use the one from Xamarin.Forms.Platform.Android.FastRenderers

If I change the code to

public class GradientStackLayout : StackLayout 
{
    // ...
}

instead of

public class GradientStackLayout : VisualElementRenderer<StackLayout>
{
    // ...
}

the compiler would complain that there are no DispatchDraw and OnElementChanged methods.

Should I use the obsolete renderer? Is there any new method to implement custom renderers?

1 Answer 1

1

You just add a construct function in class GradientColorStackRenderer in Xamarin android.

public GradientColorStackRenderer(Context context):base(context)
    {
    }

So the whole code is as follows:

[assembly: ExportRenderer(typeof(GradientColorStack), typeof(GradientColorStackRenderer))]
namespace GradientBackgroundApp.Droid
{
 public class GradientColorStackRenderer: VisualElementRenderer<StackLayout>
{

    private Color StartColor { get; set; }
    private Color EndColor { get; set; }

    // add the construct here
    public GradientColorStackRenderer(Context context):base(context)
    {
    }

    protected override void DispatchDraw(global::Android.Graphics.Canvas canvas)
    {
        #region for Vertical Gradient
        //var gradient = new Android.Graphics.LinearGradient(0, 0, 0, Height,
        #endregion

        #region for Horizontal Gradient
        var gradient = new Android.Graphics.LinearGradient(0, 0, Width, 0,
        #endregion

               this.StartColor.ToAndroid(),
               this.EndColor.ToAndroid(),
               Android.Graphics.Shader.TileMode.Mirror);

        var paint = new Android.Graphics.Paint()
        {
            Dither = true,
        };
        paint.SetShader(gradient);
        canvas.DrawPaint(paint);
        base.DispatchDraw(canvas);
    }

    protected override void OnElementChanged(ElementChangedEventArgs<StackLayout> e)
    {
        base.OnElementChanged(e);

        if (e.OldElement != null || Element == null)
        {
            return;
        }
        try
        {
            var stack = e.NewElement as GradientColorStack;
            this.StartColor = stack.StartColor;
            this.EndColor = stack.EndColor;
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(@"ERROR:", ex.Message);
        }
    }
  }
 }
Sign up to request clarification or add additional context in comments.

4 Comments

yes it worked .how do you find it? If I have questions again how should I do?
When we encounter a problem, we usually analyze the Log first and if we can't find the reason,we can try to search the Log on Google. For general problem, there will be some solutions. :)
ok.thankyou .by the way .where to see the code of the element in xamarin such as Label,Image. I can not see it in the github.
Are you talking about this link? github.com/xamarin/Xamarin.Forms

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.