Delpin Susai Raj Tuesday, 17 December 2019

Xamarin.Forms - Working with Effects

In this blog post, you will learn how to use Effect instead of Custom renderer in Xamarin.Forms.

Introduction
Xamarin.Forms code runs on multiple platforms - each of which has its own filesystem. This means that reading and writing files are 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. Effects simplify this process, allowing the native controls on each platform to be more easily customized.
Why Effects instead of Custom Renderer?
  1. An effect is when changing the properties of a platform-specific control will achieve the desired result.
  2. A custom renderer is required when there's a need to override methods of a platform-specific control.
  3. 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
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

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

iOS Implementation

In this step, create an EntryiOSEffect.cs class and inherit PlatformEffect Class for Add Effects the Entry control.

Now, write the code given below.

EntryiOSEffect.cs

Setting up the User Interface

Go to MainPage.Xaml and write the following code.

MainPage.xaml

Consuming the Effect

Here, consuming the Effect in your XAML

Add Namespace

Add Effect in Entry

Finally, check your XAML.

MainPage.XAML

Click the "Play" button to try it out.
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 😍

No comments:

Post a Comment