Effects in Xamarin Forms as really useful weapon in the Armory of Xamarin Developer

Effects in Xamarin Forms is a very awesome feature and in this blog post we will deep dive in Effects.

As per official documentation of Xamarin

Effects allow the native controls on each platform to be customized, and are typically used for small styling changes. This article provides an introduction to effects, outlines the boundary between effects and custom renderers, and describes the PlatformEffect class.

Xamarin Forms Effects are very useful for platform specific customization, Now you will ask that we can also do such stuff using Custom Renderers , because a Platform Renderer transforms Xamarin Forms object to a platform specific object as shown below

However  creating a custom renderer in order to perform a very simple control customization is really a heavy-weight response. Effects simplify this process, allowing the native controls on each platform to be more easily customized.

When we create Effect we have to crate a Effect class per platform , For example to implement a shadow effect our classes amy look like these.

Now the Million $ Question is

Why to write an Effect while same can be done by Custom Renderer ?

Well reasons are

  • Effects can very much simplify the customization of a control
  • Effects are reusable
  • Effects can be parameterized to further increase reusability.

Any customization that can be achieved with an effect can also be achieved with a custom renderer. But, custom renderers offer more flexibility and customization than effects. The following guidelines list the circumstances in which to choose an effect over a custom renderer:

  • An effect is recommended 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.

The PlatformEffect Class

Here is the list ofnamespace for the PlatformEffect class on each platform:

Platform Namespace Container Control
iOS Xamarin.Forms.Platform.iOS UIView UIView
Android Xamarin.Forms.Platform.Android ViewGroup View
 UWP Xamarin.Forms.Platform.UWP FrameworkElement FrameworkElement

Each platform specific PlatformEffect class exposes the following properties:
Container – references the platform-specific control being used to implement the layout.
Control – references the platform-specific control being used to implement the Xamarin.Forms control.
Element – references the Xamarin.Forms control that’s being rendered.

Every Xamarin Forms Element have a collection that can hold all effects of that Element.

We are required to give a unique name to an effect so that we can resolve it. Once a effect is resolved we can add it in the Effects collection of a Xamarin Forms element but we can not use the same effect in another element again, we need to do that we need to resolve it again.

Effect eff = Effect.Resolve(“MyApp.ShadowEffect”));
OneButton.Effects.Add(eff); // ok
AnotherButton.Effects.Add(eff); // runtime error

Each platform-specific PlatformEffect class exposes the following methods, which must be overridden to implement an effect:

  • OnAttached called when an affect is attached to a Xamarin.Forms control. An overridden version of this method, in each platfom-specific effect class, is the place to perform customization of the control, along with exception handling in case the effect cannot be applied to the specified Xamarin.Forms control.
  • OnDetached called when an effect is detached from a Xamarin.Forms control. An overridden version of this method, in each platform-specific effect class, is the place to perform any effect cleanup such as de-registering an event handler.

Summary

Effects are really very useful because they allow the native controls on each platform to be customized without having to implement a massive custom renderer for simple and small cosmetic changes.  Power of Effects can be even further increased for reusability by using parameters in Effects.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: