Xamarin.Forms - Border Shadow Effects Using Custom Frame Renderer

Introduction

Xamarin is a platform to develop cross-platform and multi-platform apps (for example, Windows Phone, Android, iOS). In Xamarin platform, the code sharing concept is used. In Xamarin Studio, Visual Studio is also available.
 
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.
 
 
Prerequisites
  • Visual Studio 2017(Windows or Mac)
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.

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.

 

Creating Your Model(ListValue)


ListValue.cs
  1. using System;  
  2. namespace XamarinForms_Frame {  
  3.     public class ListValue {  
  4.         public string Title {  
  5.             get;  
  6.             set;  
  7.         }  
  8.         public string Content {  
  9.             get;  
  10.             set;  
  11.         }  
  12.     }  
  13. }  
Setting up the User Interface.
 
 
Go MainPage.Xaml and write following code.

MainPage.Xaml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:XamarinForms_Frame" x:Class="XamarinForms_Frame.XamarinForms_FramePage">  
  3.     <Label Text="Welcome to Xamarin Forms!" VerticalOptions="Center" HorizontalOptions="Center" />  
  4.     <ContentView>  
  5.         <StackLayout Margin="50" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand">  
  6.             <ListView x:Name="lstValue" HorizontalOptions="Center" VerticalOptions="Center" Margin="10,10,10,0" SeparatorVisibility="None">  
  7.                 <ListView.ItemTemplate>  
  8.                     <DataTemplate>  
  9.                         <ViewCell>  
  10.                             <StackLayout>  
  11.                                 <Label Text="{Binding Title}"></Label> </StackLayout>  
  12.                         </ViewCell>  
  13.                     </DataTemplate>  
  14.                 </ListView.ItemTemplate>  
  15.             </ListView>  
  16.         </StackLayout>  
  17.     </ContentView>  
  18. </ContentPage>  
In this Step Add Data to Listview.

 
MainPage.Xaml.cs
  1. using System.Collections.Generic;  
  2. using Xamarin.Forms;  
  3. using XamarinForms_Frame;  
  4. namespace XamarinForms_Frame {  
  5.     public partial class XamarinForms_FramePage: ContentPage {  
  6.         List < ListValue > list = new List < ListValue > ();  
  7.         public XamarinForms_FramePage() {  
  8.             InitializeComponent();  
  9.             list.Add(new ListValue() {  
  10.                 Title = "Xamarin Android"  
  11.             });  
  12.             list.Add(new ListValue() {  
  13.                 Title = "Xamarin iOS"  
  14.             });  
  15.             list.Add(new ListValue() {  
  16.                 Title = "Xamarin.Forms"  
  17.             });  
  18.             lstValue.ItemsSource = list;  
  19.         }  
  20.     }  
  21. }  
Now, Create a Frame Inherit class for customise the Frame control.
 
Go to Solution—>Empty Class—> filename

 
 
MyFrame.cs 
  1. using System;  
  2. using Xamarin.Forms;  
  3. namespace XamarinForms_Frame {  
  4.     public class MyFrame: Frame {  
  5.         public MyFrame() {}  
  6.     }  
  7. }  
 

Making Your Android Frame Shadows

In this step Create xml file rounded design for customizing frame control.

Go to Solution.Droid—>Empty XML File—> FrameRenderValue.xml. then click Create.



Now, Write the following code in your XML file.

 
 
FrameRenderValue.xml
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">  
  3.     <stroke android:width="1dp" android:color="#252525" />  
  4.     <corners android:radius="5dp" />  
  5.     <solid android:color="#FFFFFF" />  
  6. </shape>  
In this step, create a Android FrameRenderer inherit class for customizing frame control.

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

 
 
Now write the following code between OnElementChanged override method.

 
 
MyFrameRender.cs
  1. using Android.Graphics;  
  2. using Android.Graphics.Drawables;  
  3. using Xamarin.Forms;  
  4. using Xamarin.Forms.Platform.Android;  
  5. using XamarinForms_Frame;  
  6. using XamarinForms_Frame.Droid;  
  7. [assembly: ExportRenderer(typeof(MyFrame), typeof(MyFrameRender))]  
  8. namespace XamarinForms_Frame.Droid {  
  9.     public class MyFrameRender: FrameRenderer {  
  10.         protected override void OnElementChanged(ElementChangedEventArgs < Frame > e) {  
  11.             base.OnElementChanged(e);  
  12.             if (e.NewElement != null && e.OldElement == null) {  
  13.                 this.SetBackgroundResource(Resource.Drawable.FrameRenderValue);  
  14.                 GradientDrawable drawable = (GradientDrawable) this.Background;  
  15.                 drawable.SetColor(Android.Graphics.Color.ParseColor("#F0F0F0"));  
  16.             }  
  17.         }  
  18.     }  
  19. }  
Making Your iOS Frame Shadows

In this step Create a iOS FrameRenderer inherit class for customizing frame control

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

 
 
Now write the following code between OnElementChanged override method.

 

MyFrameRender.cs
  1. using System;  
  2. using CoreGraphics;  
  3. using UIKit;  
  4. using Xamarin.Forms;  
  5. using Xamarin.Forms.Platform.iOS;  
  6. using XamarinForms_Frame;  
  7. [assembly: ExportRenderer(typeof(MyFrame), typeof(XamarinForms_Frame.iOS.MyFrameRender))]  
  8. namespace XamarinForms_Frame.iOS {  
  9.     public class MyFrameRender: FrameRenderer {  
  10.         protected override void OnElementChanged(ElementChangedEventArgs < Frame > e) {  
  11.             base.OnElementChanged(e);  
  12.             Layer.BorderColor = UIColor.White.CGColor;  
  13.             Layer.CornerRadius = 10;  
  14.             Layer.MasksToBounds = false;  
  15.             Layer.ShadowOffset = new CGSize(-2, 2);  
  16.             Layer.ShadowRadius = 5;  
  17.             Layer.ShadowOpacity = 0.4 f;  
  18.         }  
  19.     }  
  20. }  
Now Add Customise frame control in your app.

 
 
MainPage.xaml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:XamarinForms_Frame" x:Class="XamarinForms_Frame.XamarinForms_FramePage" xmlns:controls="clr-namespace:XamarinForms_Frame;assembly=XamarinForms_Frame">  
  3.     <Label Text="Welcome to Xamarin Forms!" VerticalOptions="Center" HorizontalOptions="Center" />  
  4.     <ContentView>  
  5.         <StackLayout Margin="50" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand">  
  6.             <ListView x:Name="lstValue" HorizontalOptions="Center" VerticalOptions="Center" Margin="10,10,10,0" SeparatorVisibility="None">  
  7.                 <ListView.ItemTemplate>  
  8.                     <DataTemplate>  
  9.                         <ViewCell>  
  10.                             <controls:MyFrame HasShadow="true" OutlineColor="Silver">  
  11.                                 <StackLayout>  
  12.                                     <Label Text="{Binding Title}"></Label> </StackLayout>  
  13.                             </controls:MyFrame>  
  14.                         </ViewCell>  
  15.                     </DataTemplate>  
  16.                 </ListView.ItemTemplate>  
  17.             </ListView>  
  18.         </StackLayout>  
  19.     </ContentView>  
  20. </ContentPage>  
Click the play button to try it out.

You can see the Wonderful Border shadow Effect using frame renderer in your Controls.

 

Summary

This was the process of how to use Border Shadow effect using Custom Frame Renderer in your controls in Xamarin.Forms.

Up Next
    Ebook Download
    View all
    Learn
    View all