Introduction
Xamarin is a platform to develop cross-platform and multi-platform apps (write once and access to multiple platforms).
Here, we are going to create a FrameRenderer in Xamarin.Forms.
Frame
- HasShadow. = true or false, to indicate whether to show a shadow effect where the platform supports it.
- OutlineColor = A color specification, with or without the prefix, "Color". For example, "Color.Red" and "Red" both specify the color red.
Steps
The steps given below are required to be followed in order to use Border Shadow effect using Frame Renderer in your controls in Xamarin forms, using Visual Studio.
This way, you can create a project for Visual Studio.
File -> New Project.
Step 2
Select BlankApp and .NET Standard.
MainPage.xaml
MainPage.xaml.cs
Create a Class (MyFramee.cs) Model.
Add this code to the MyFramee,
- namespace Custom_FrameeRenderer {
- publicclassMyFramee: Xamarin.Forms.Frame {
- public MyFramee() {}
- }
- }
Create a class in Custom_FrameeRenderer.Droid -> FrameRendererr
Then, add a code in this Class.
- publicclassRoundBorderFrameRenderer: FrameRenderer {
- public RoundBorderFrameRenderer(Context context): base(context) {}
- publicoverridevoid Draw(Canvas canvas) {
- base.Draw(canvas);
- if (Element == null || Element.OutlineColor.A <= 0) {
- return;
- }
- using(var paint = new Paint {
- AntiAlias = true
- })
- using(var path = new Path())
- using(Path.Direction direction = Path.Direction.Cw)
- using(Paint.Style style = Paint.Style.Stroke)
- using(var rect = new RectF(0, 0, canvas.Width, canvas.Height)) {
- var raduis = Android.App.Application.Context.ToPixels(Element.CornerRadius);
- path.AddRoundRect(rect, raduis, raduis, direction);
-
- paint.SetStyle(style);
- paint.Color = Element.OutlineColor.ToAndroid();
- canvas.DrawPath(path, paint);
- }
- }
- }
Create a local namespace in MainPage.
- xmlns:local="clr-namespace:NotesFinalPage;assembly=NotesFinalPage"
Then, Create.
- <local:MyFramee outlinecolor=”blue” cornerRadius=”5”>
- <Label Text=”FrameRenderer” HeightRequest=”10”/>
- </ local:MyFramee>
Summary
This was the process of how to use Border Shadow effect using Custom Frame Renderer in your controls in Xamarin.Forms.