Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

RoundedBoxView custom renderer from Xamarin University video no longer working

$
0
0

I made the RoundedBoxView that was in the Xamarin University: Custom Renderers in Xamarin.Forms video. It was working before, but it seems to no longer work (at least for iOS). The stroke thickness and stroke color still work, but it doesn't actually round the box view anymore. I'm thinking it has to do with the new unified api for iOS. Anyone know how to make it work again?

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 ();
        }
    }
}

Viewing all articles
Browse latest Browse all 58056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>