locked
FrameRenderer crash with Binding RRS feed

  • Question

  • User380273 posted

    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:

    1. 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);
          }
      }
      
    2. CustomControl

    public class FrameGradient : Frame

    {
    
        public Color StartColor { get; set; }
        public Color EndColor { get; set; }
    
    }
    
    1. Xaml

    Thursday, December 13, 2018 11:54 AM

All replies