I am using the RoundedBoxView custom renderer that was shown in the Xamarin University: Custom Renderers in Xamarin.Forms video, but it no longer seems to work completely. The stroke etc works, but the corner radius no longer works. Anyone know how to fix this?
Here is my code:
RoundedBoxView.cs
using System;
using Xamarin.Forms;
namespace MyApp.Controls
{
public class RoundedBoxView : BoxView
{
public static readonly BindableProperty CornerRadiusProperty =
BindableProperty.Create<RoundedBoxView, double>(p => p.CornerRadius, 0);
public double CornerRadius
{
get { return (double)GetValue (CornerRadiusProperty); }
set { SetValue (CornerRadiusProperty, value); }
}
public static readonly BindableProperty StrokeProperty =
BindableProperty.Create<RoundedBoxView, Color>(p => p.Stroke, Color.Transparent);
public Color Stroke
{
get { return (Color)GetValue (StrokeProperty); }
set { SetValue (StrokeProperty, value); }
}
public static readonly BindableProperty StrokeThicknessProperty =
BindableProperty.Create<RoundedBoxView, double>(p => p.StrokeThickness, 0);
public double StrokeThickness
{
get { return (double)GetValue (StrokeThicknessProperty); }
set { SetValue (StrokeThicknessProperty, value); }
}
}
}
RoundedBoxViewRenderer.cs
using MyApp.Controls;
using System;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using UIKit;
using CoreGraphics;
using CoreLocation;
using MyApp.iOS;
using MyApp;
[assembly: ExportRendererAttribute(typeof(RoundedBoxView), typeof(RoundedBoxViewRenderer))]
namespace MyApp.iOS
{
public class RoundedBoxViewRenderer : BoxRenderer
{
public override void Draw (CGRect rect)
{
RoundedBoxView rbv = (RoundedBoxView)this.Element;
using (var context = UIGraphics.GetCurrentContext ()) {
context.SetFillColor(rbv.Color.ToCGColor());
context.SetStrokeColor (rbv.Stroke.ToCGColor ());
context.SetLineWidth ((float)rbv.StrokeThickness);
var rc = this.Bounds.Inset ((int)rbv.StrokeThickness, (int)rbv.StrokeThickness);
nfloat radius = (nfloat)rbv.CornerRadius;
radius = (nfloat)Math.Max (0, Math.Min (radius, Math.Max (rc.Height / 2, rc.Width / 2)));
var path = CGPath.FromRoundedRect (rc, radius, radius);
context.AddPath (path);
context.DrawPath (CGPathDrawingMode.FillStroke);
}
}
protected override void OnElementPropertyChanged (object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged (sender, e);
if (e.PropertyName == RoundedBoxView.CornerRadiusProperty.PropertyName || e.PropertyName == RoundedBoxView.StrokeProperty.PropertyName || e.PropertyName == RoundedBoxView.StrokeThicknessProperty.PropertyName)
this.SetNeedsDisplay ();
}
}
}