Xamarin.Forms - Working With Effects

Introduction

 
Woring with Effects
 
Xamarin.Forms code runs on multiple platforms - each of which has its own filesystem. This means that reading and writing files is most easily done using the native file APIs on each platform. Alternatively, embedded resources are a simpler solution to distribute data files with an app.
 

Effects

 
Effects simplify the customization of control.
 
Developers can implement their own custom Renderer classes to customize the appearance and/or behavior of a control. However, implementing a custom renderer class to perform simple control customization is often a heavy-weight endeavor. Effects simplify this process, allowing the native controls on each platform to be more easily customized.
 
Woring with Effects
 

Why Effects instead of Custom Renderer?

  • An effect is when changing the properties of a platform-specific control will achieve the desired result.
  • A custom renderer is required when there's a need to override methods of a platform-specific control.
  • A custom renderer is required when there's a need to replace the platform-specific control that implements a Xamarin.Forms control.
Prerequisites
  • Visual Studio 2017 or later (Windows or Mac)

Setting up a Xamarin.Forms Project(Windows)

 
Start by creating a new Xamarin.Forms project. You wíll learn more by going through the steps yourself.
 
Now, you need to click "Create a new project".
 
Next, filter by Project Type: Mobile
 
Woring with Effects
 
Choose the Mobile App (Xamarin. forms) project under C# and Mobile.
 
Name your app. You probably want your project and solution to use the same name as your app. Put it on your preferred location for projects and click "Create".
 
Now, select the blank app and target platforms - Android, iOS and Windows (UWP).
 
Subsequently, go to the solution. In there, you get all the files and sources of your project (.NET Standard). Now, select the XAML page and double-click to open the MainPage.Xaml page.
 
You now have a basic Xamarin.Forms app. Click the Play button to try it out.
 

Create a Borderless Entry Using Effect

 
Now, create an EntryEffect class and Inherit RoutingEffect class for add Effect the Entry control.
 
Now, write the following code.
 
EntryEffect.cs
  1. using Xamarin.Forms;  
  2.   
  3. namespace XamarinStudy.Common.Controls  
  4. {  
  5.     public class EntryEffect: RoutingEffect  
  6.     {  
  7.         public EntryEffect() : base($"MyCompany.{nameof(EntryEffect)}")  
  8.         {  
  9.         }  
  10.     }  
  11. }  

Android Implementation

 
In this step, create an EntryAndroidEffect.cs class and inherit PlatformEffect Class for Add Effects the Entry control.
  • OnAttached - Override the OnAttached method and write logic to customize the control.
  • OnDetached - Override the OnDetached method and write logic to clean up the control customization, if required. 
Now, write the code given below.
 
EntryAndroidEffect.cs
  1. [assembly: ResolutionGroupName("MyCompany")]  
  2. [assembly: ExportEffect(typeof(XamarinStudy.Droid.Effects.EntryAndroidEffect), nameof(EntryEffect))]  
  3. namespace XamarinStudy.Droid.Effects  
  4. {  
  5.     public class EntryAndroidEffect:PlatformEffect  
  6.     {  
  7.   
  8.         protected override void OnAttached()  
  9.         {  
  10.             if(this.Control!=null)  
  11.             {  
  12.                 this.Control.Background = new ColorDrawable(Android.Graphics.Color.Transparent);  
  13.             }  
  14.         }  
  15.   
  16.         protected override void OnDetached()  
  17.         {  
  18.             throw new NotImplementedException();  
  19.         }  
  20.     }  
  21. }  

iOS Implementation

 
In this step, create an EntryiOSEffect.cs class and inherit PlatformEffect Class for adding effects for  the entry control.
 
Now, write the code given below.
 
EntryiOSEffect.cs
  1. [assembly: ResolutionGroupName("MyCompany")]  
  2. [assembly: ExportEffect(typeof(XamarinStudy.iOS.Effects.EntryiOSEffect), nameof(EntryEffect))]  
  3. namespace XamarinStudy.iOS.Effects  
  4. {  
  5.     public class EntryiOSEffect : PlatformEffect  
  6.     {  
  7.         protected override void OnAttached()  
  8.         {  
  9.             if(this.Control!=null)  
  10.             {  
  11.                 var textField = (UITextField)this.Control;  
  12.                 textField.BorderStyle = UITextBorderStyle.None;  
  13.             }  
  14.         }  
  15.   
  16.         protected override void OnDetached()  
  17.         {  
  18.             throw new NotImplementedException();  
  19.         }  
  20.     }  
  21. }  

Setting up the User Interface

 
Go to MainPage.Xaml and write the following code.
 
MainPage.xaml
  1. <?xml version="1.0" encoding="utf-8" ?>    
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"    
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"    
  4.              xmlns:d="http://xamarin.com/schemas/2014/forms/design"    
  5.              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"    
  6.              mc:Ignorable="d"    
  7.              x:Class="XamarinStudy.MainPage" Title="Effects">    
  8.     
  9.     <StackLayout Margin="10,30,10,0" Spacing="20">    
  10.         <Image Source="banner.png"/>    
  11.         <Label Text="Effects" HorizontalOptions="CenterAndExpand" FontSize="Large"/>    
  12.         <Entry  HeightRequest="50"  Placeholder="Normal Entry"/>    
  13.     </StackLayout>    
  14.     
  15. </ContentPage>    

Consuming the Effect

 
Here, consume the Effect in your XAML
 
Add Namespace
  1. xmlns:Effects="clr-namespace:XamarinStudy.Common.Controls"  
Add Effect in Entry
  1. <Entry  HeightRequest="50"  BackgroundColor="LightGray" PlaceholderColor="Black" Placeholder="Effect Used Entry">    
  2.             <Entry.Effects>    
  3.                 <Effects:EntryEffect/>    
  4.             </Entry.Effects>    
  5.         </Entry>     
Finally, check your XAML.
 
MainPage.XAML
  1. <?xml version="1.0" encoding="utf-8" ?>    
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"    
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"    
  4.              xmlns:d="http://xamarin.com/schemas/2014/forms/design"    
  5.              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"    
  6.              mc:Ignorable="d"    
  7.              xmlns:Effects="clr-namespace:XamarinStudy.Common.Controls"    
  8.              x:Class="XamarinStudy.MainPage" Title="Effects">    
  9.     
  10.     <StackLayout Margin="10,30,10,0" Spacing="20">    
  11.         <Image Source="banner.png"/>    
  12.         <Label Text="Effects" HorizontalOptions="CenterAndExpand" FontSize="Large"/>    
  13.         <Entry  HeightRequest="50"  Placeholder="Normal Entry"/>    
  14.         <Entry  HeightRequest="50"  BackgroundColor="LightGray" PlaceholderColor="Black" Placeholder="Effect Used Entry">    
  15.             <Entry.Effects>    
  16.                 <Effects:EntryEffect/>    
  17.             </Entry.Effects>    
  18.         </Entry>    
  19.     </StackLayout>    
  20.         
  21. </ContentPage>    
Click the "Play" button to try it out.
 
Woring with Effects
 
I hope you have understood how to use Effect instead of Custom renderer in Xamarin.Forms.
 
Thanks for reading. Please share your comments and feedback. Happy Coding