Delpin Susai Raj Monday, 3 February 2020

Xamarin.Forms - Use BindableProperty in Effects

In this blog, you will learn how to use BindableProperty Effects in Xamarin.Forms


Tint Color Sample -  https://xamarinmonkeys.blogspot.com/2020/02/xamarinforms-tint-image-using-effects.html

(The Blog about change Image color at Runtime using Tint Color)


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.

Tint Image

TintImage is used for change image color at runtime with the help of effects, you can achieve with custom render also. Tint images mainly used to reduce the usage of the images.

BindableProperty

Attached properties can be used to define effect parameters that respond to runtime property changes. This article demonstrates using attached properties to pass parameters to an effect and changing a parameter at runtime.

Prerequisites
  • Visual Studio 2017 or later (Windows or Mac)
The Problem

We are not able to create a BindableProperty in Effect.

Solution

Create Effect

Now, Create a TintImage class inherit from RoutingEffect for Change image color.

TintImage.cs

Create Static Class

Here, I'm Going to create a Static Class for creating BindableProperty to my Effect.

TintImageEffect.cs

Android Implementation

Here, Implement Platform Effects for TintImage at Runtime.

TintImageImpl.cs

iOS Implementation

Here, Implement Platform Effects for TintImage at Runtime.

TintImageImpl.cs

Create ViewModel

Now, Create a Property for Binding the TintColor to View. and Create ChangeColorCommand For Change image color at runtime.

MainPageViewModel.cs

Consuming the TintImage

Now, Use the TintImage Effect in your XAML code.

MainPage.xaml

Click the "Play" button to try it out.

Wow, It's Working.😍

I hope you have understood how to use BindableProperty Effects in Xamarin.Forms.

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