Effects
Introduction#
Effects simplifies platform specific customizations. When there is a need to modify a Xamarin Forms Control’s properties, Effects can be used. When there is a need to override the Xamarin Forms Control’s methods, Custom renderers can be used
Adding platform specific Effect for an Entry control
- Create a new Xamarin Forms app using PCL File -> New Solution -> Multiplatform App -> Xamarin Forms -> Forms App; Name the project as
EffectsDemo
- Under the iOS project, add a new
Effect
class that inherits fromPlatformEffect
class and overrides the methodsOnAttached
,OnDetached
andOnElementPropertyChanged
Notice the two attributesResolutionGroupName
andExportEffect
, these are required for consuming this effect from the PCL/shared project.
-
OnAttached
is the method where the logic for customization goes in -
OnDetached
is the method where the clean up and de-registering happens -
OnElementPropertyChanged
is the method which gets triggered upon property changes of different elements. To identify the right property, check for the exact property change and add your logic. In this example,OnFocus
will give theBlue
color andOutofFocus
will giveRed
Colorusing System; using EffectsDemo.iOS; using UIKit; using Xamarin.Forms; using Xamarin.Forms.Platform.iOS; [assembly: ResolutionGroupName("xhackers")] [assembly: ExportEffect(typeof(FocusEffect), "FocusEffect")] namespace EffectsDemo.iOS { public class FocusEffect : PlatformEffect { public FocusEffect() { } UIColor backgroundColor; protected override void OnAttached() { try { Control.BackgroundColor = backgroundColor = UIColor.Red; } catch (Exception ex) { Console.WriteLine("Cannot set attacked property" + ex.Message); } } protected override void OnDetached() { throw new NotImplementedException(); } protected override void OnElementPropertyChanged(System.ComponentModel.PropertyChangedEventArgs args) { base.OnElementPropertyChanged(args); try { if (args.PropertyName == "IsFocused") { if (Control.BackgroundColor == backgroundColor) { Control.BackgroundColor = UIColor.Blue; } else { Control.BackgroundColor = backgroundColor; } } } catch (Exception ex) { Console.WriteLine("Cannot set property " + ex.Message); } }
}
}
-
To Consume this effect in the application, Under the
PCL
project, create a new class namedFocusEffect
which inherits fromRoutingEffect
. This is essential to make the PCL instantiate the platform specific implementation of the effect. Sample code below:using Xamarin.Forms; namespace EffectsDemo { public class FocusEffect : RoutingEffect { public FocusEffect() : base("xhackers.FocusEffect") { } } }
-
Add the effect to
Entry
control in the XAML<?xml version="1.0" encoding="utf-8"?> <ContentPage xmlns="https://xamarin.com/schemas/2014/forms" xmlns:x="https://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:EffectsDemo" x:Class="EffectsDemo.EffectsDemoPage"> <StackLayout Orientation="Horizontal" HorizontalOptions="Center" VerticalOptions="Center"> <Label Text="Effects Demo" HorizontalOptions="StartAndExpand" VerticalOptions="Center" ></Label> <Entry Text="Controlled by effects" HorizontalOptions="FillAndExpand" VerticalOptions="Center"> <Entry.Effects> <local:FocusEffect> </local:FocusEffect> </Entry.Effects> </Entry> </StackLayout> </ContentPage>
Since the Effect was implemented only in iOS version, when the app runs in iOS Simulator
upon focusing the Entry
background color changes and nothing happens in Android Emulator
as the Effect
wasn’t created under Droid
project