Hy, i'd create a Android Custom control for frame to create a gradient background. It works fine if i set static color(StartColor and EndColor) but if i bind with converters, app crashes. This is my code:
Renderer
public class FrameGradientRenderer : VisualElementRenderer { private Color StartColor { get; set; } private Color EndColor { get; set; } protected override void DispatchDraw(global::Android.Graphics.Canvas canvas) { var gradient = new Android.Graphics.LinearGradient(0, 0, Width, 0, 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<Frame> e) { base.OnElementChanged(e); if (e.OldElement != null || Element == null) { return; } try { var frame = e.NewElement as FrameGradient; this.StartColor = frame.StartColor; this.EndColor = frame.EndColor; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(@"ERROR:", ex.Message); } }
CustomControl
:
public class FrameGradient : Frame { public Color StartColor { get; set; } public Color EndColor { get; set; } }
How did you use converters in your project? What is your crash log? If possible, could you please post more detailed codes? So that we can reproduce your issue on our side.
As an alternative choice, you could set the gradient background via .xml
file.
First, create your gradient background under Drawable
folder of Android resources. For example:
<?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <gradient android:angle="90" android:centerColor="#555994" android:endColor="#b5b6d2" android:startColor="#555994" android:type="linear" /> <corners android:radius="0dp"/> </shape>
Name this file as "my_gradient.xml" and place it under the Drawable
folder of Android project, then in your FrameGradientRenderer
:
public class FrameGradientRenderer : Xamarin.Forms.Platform.Android.AppCompat.FrameRenderer { public FrameGradientRenderer(Context context) : base(context) { } protected override void OnElementChanged(ElementChangedEventArgs<Frame> e) { base.OnElementChanged(e); if (e.NewElement != null) { this.SetBackgroundResource(Resource.Drawable.my_gradient_drawable); } } }
Answers
How did you use converters in your project? What is your crash log? If possible, could you please post more detailed codes? So that we can reproduce your issue on our side.
As an alternative choice, you could set the gradient background via
.xml
file.First, create your gradient background under
Drawable
folder of Android resources. For example:Name this file as "my_gradient.xml" and place it under the
Drawable
folder of Android project, then in yourFrameGradientRenderer
:Awesome! It works great. I don't use anymore converters beacuse now i do all i need in "OnElementChanged". Thank you
@GLFichera, if this can help you resolve this issue, can you mark it as an answer?
already done
@GLFichera , happy coding.