Xamarin.Forms - Rounded BoxView Using Custom BoxRenderer

Introduction

BoxView renders a simple rectangle of a specified width, height, and color. You can use BoxView for decoration, rudimentary graphics, and for interaction with the user through touch.

Because Xamarin.Forms does not have a built-in vector graphics system, the BoxView helps to compensate. Some of the sample programs described in this article use BoxView for rendering graphics. The BoxView can be sized to resemble a line of a specific width and thickness, and then rotated by any angle using the Rotation property.

Custom Renderers

Xamarin.Forms user interfaces are rendered using the native controls of the target platform, allowing Xamarin.Forms applications to retain the appropriate look and feel for each platform. Custom Renderers let developers override this process to customize the appearance and behavior of Xamarin.Forms controls on each platform.
 
 
Prerequisites
  • Visual Studio 2017(Windows or Mac)
The steps given below are required to be followed in order to Create Rounded BoxView Using BoxRenderer in your controls in Xamarin.Forms, using Visual Studio.

Setting up a Xamarin.Forms Project

Start by creating a new Xamarin.Forms project. You’ll learn more by going through the steps yourself.

Choose the Xamarin.Forms App Project type under Cross-platform/App in the New Project dialog.

 
 
Name your app, select “Use Portable Class Library” for shared code, and target both Android and iOS.

 

You probably want your project and solution to use the same name as your app. Put it in your preferred folder for projects and click Create.

 

You now have a basic Xamarin.Forms app. Click the play button to try it out.

 
 
Create a Custom BoxView

Now, Create an Inherit class form BoxView for Customizing BoxView control.

Go to Solution—>Empty Class—> filename

 
 
Now, write the following code

MyBoxView.cs
  1. using System;  
  2. using Xamarin.Forms;  
  3. namespace XamarinForms_RBoxView {  
  4.     public class MyBoxView: BoxView {  
  5.         //Create a Bindable Property For CornerRadius  
  6.         public static readonly BindableProperty CornerRadiusProperty = BindableProperty.Create(nameof(CornerRadius), typeof(double), typeof(MyBoxView), 0.0);  
  7.         public double CornerRadius {  
  8.             get {  
  9.                 return (double) GetValue(CornerRadiusProperty);  
  10.             }  
  11.             set {  
  12.                 SetValue(CornerRadiusProperty, value);  
  13.             }  
  14.         }  
  15.     }  
  16. }  


Making Your Android Implementation

In this step create an inherit Class form, BoxRenderer, for customizing BoxView control

Go to Solution.Droid—>Empty Class—> RoundBoxViewRender

 
 
Now, write the code given below.

RoundBoxViewRender.cs
  1. using System;  
  2. using Android.Graphics;  
  3. using Android.Util;  
  4. using Xamarin.Forms;  
  5. using Xamarin.Forms.Platform.Android;  
  6. using XamarinForms_RBoxView;  
  7. using XamarinForms_RBoxView.Droid;  
  8. [assembly: ExportRenderer(typeof(MyBoxView), typeof(RoundBoxViewRender))]  
  9. namespace XamarinForms_RBoxView.Droid {  
  10.     public class RoundBoxViewRender: BoxRenderer {  
  11.         private float _cornerRadius;  
  12.         private RectF _bounds;  
  13.         private Path _path;  
  14.         protected override void OnElementChanged(ElementChangedEventArgs < BoxView > e) {  
  15.             base.OnElementChanged(e);  
  16.             if (Element == null) {  
  17.                 return;  
  18.             }  
  19.             var element = (MyBoxView) Element;  
  20.             _cornerRadius = TypedValue.ApplyDimension(ComplexUnitType.Dip, (float) element.CornerRadius, Context.Resources.DisplayMetrics);  
  21.         }  
  22.         protected override void OnSizeChanged(int w, int h, int oldw, int oldh) {  
  23.             base.OnSizeChanged(w, h, oldw, oldh);  
  24.             if (w != oldw && h != oldh) {  
  25.                 _bounds = new RectF(0, 0, w, h);  
  26.             }  
  27.             _path = new Path();  
  28.             _path.Reset();  
  29.             _path.AddRoundRect(_bounds, _cornerRadius, _cornerRadius, Path.Direction.Cw);  
  30.             _path.Close();  
  31.         }  
  32.         public override void Draw(Canvas canvas) {  
  33.             canvas.Save();  
  34.             canvas.ClipPath(_path);  
  35.             base.Draw(canvas);  
  36.             canvas.Restore();  
  37.         }  
  38.     }  
  39. }  


Making Your iOS Implementation

In this step Create a inherit Class form BoxRenderer for customizing BoxView control

Go to Solution.iOS—>Empty Class—> RoundBoxViewRender

 
 
Now, write the code given below.

RoundBoxViewRender.cs
  1. using System;  
  2. using Xamarin.Forms;  
  3. using Xamarin.Forms.Platform.iOS;  
  4. using XamarinForms_RBoxView;  
  5. using XamarinForms_RBoxView.iOS;  
  6. [assembly: ExportRenderer(typeof(MyBoxView), typeof(RoundBoxViewRender))]  
  7. namespace XamarinForms_RBoxView.iOS {  
  8.     public class RoundBoxViewRender: BoxRenderer {  
  9.         protected override void OnElementChanged(ElementChangedEventArgs < BoxView > e) {  
  10.             base.OnElementChanged(e);  
  11.             if (Element == nullreturn;  
  12.             Layer.MasksToBounds = true;  
  13.             Layer.CornerRadius = (float)((MyBoxView) this.Element).CornerRadius / 2.0 f;  
  14.         }  
  15.     }  
  16. }  
 

Setting up the User Interface.
 
Now Add Customized BoxView control in your app. Go to MainPage.Xaml and write the following code.

MainPage.xaml
  1. <local:MyBoxView Margin="0,50,0,0" VerticalOptions="End"  
  2.    HorizontalOptions="Center"  
  3.    BackgroundColor="Blue"  
  4.    CornerRadius="50"  
  5.    WidthRequest=“300”  
  6.    HeightRequest="100">  
  7. </local:MyBoxView>  
 
 
 
 
Oval Shape
  1. <local:MyBoxView VerticalOptions="Center"  
  2.    HorizontalOptions="Center"  
  3.    BackgroundColor="Blue"  
  4.    CornerRadius="100"  
  5.    WidthRequest="100"  
  6.    HeightRequest="100">  
  7. </local:MyBoxView>  
 
 
 
 
Diamond Shape 
  1. <local:MyBoxView VerticalOptions="Start" HorizontalOptions="Center" BackgroundColor="Blue" CornerRadius="900" WidthRequest="300" HeightRequest="300">  
  2. </local:MyBoxView>  
 
 
 
 
Click the play button to try it out.

 
 
You can see the Rounded BoxView Using Custom BoxViewRenderer in your Controls.

Summary

This was the process of how to Create Rounded BoxView Using BoxRenderer in Xamarin.Forms.

Thanks For Reading.

Please share comments and feedback.


Similar Articles