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 😍
Delpin Susai Raj Tuesday, 3 December 2019

Xamarin.Forms - Converter in MVVM using IValueConverter

In this blog post, you will learn how to use Converter in MVVM using IValueConverter in Xamarin.Forms.


Introduction

Xamarin.Forms - Working With Triggers
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.

Value Converter

Use Converter instead of Trigger. Data bindings usually transfer data from a source property to a target property, and in some cases from the target property to the source property. This transfer is straightforward when the source and target properties are of the same type, or when one type can be converted to the other type through an implicit conversion. When that is not the case, a type of conversion must take place.

Prerequisites
  • Visual Studio 2017 or later (Windows or Mac)
Setting up a Xamarin.Forms Project

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

Create a new or existing Xamarin forms(.Net standard) Project. With Android and iOS Platform.
Create IntToBoolConverer 

Now, Create an IntToBoolConverer class Derived from IValueConveter

I implemented IntToBoolConverter If value !=0 I return true or else false. Whatever logic you need you can implement here.

IntToBoolConverter.cs

Setting up the User Interface

Here, Use IntToBoolConverter in your XAML.

Add Namespace

Add Static Resource

MainPage.Xaml 

Click the "Play" button to try it out.

ValueToColorConveter

In this converter, I used to change color by given values.  If value = 0 Red color else Green color.

ValueToColorConveter.cs


Whatever you need to write conditions in XAML use Converter. My suggestion use converter instead of Triggers.

I hope you have understood how to use Converter in MVVM using IValueConverter in Xamarin.Forms.

Thanks for reading. Please share your comments and feedback. Happy Coding :)