(The Blog about change Image color at Run time 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 are 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)
Setting up a Xamarin.Forms Project
Start by creating a new Xamarin.Forms project. You will learn more by going through the steps yourself.
Create a new or existing Xamarin forms (.Net standard) Project with Android and iOS Platform.
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
- public class TintImage: RoutingEffect
- {
- public Color TintColor { get; private set; }
- public TintImage(Color color) : base($"MyCompany.{nameof(TintImage)}")
- {
- TintColor = color;
- }
-
- }
Create Static Class
Here, I'm going to create a Static Class to create a BindableProperty on my Effect.
TintImageEffect.cs
- public static class TintImageEffect
- {
- public static BindableProperty TintColorProperty =
- BindableProperty.CreateAttached("TintColor",
- typeof(Color),
- typeof(TintImageEffect),
- Color.Default,
- propertyChanged: OnTintColorPropertyPropertyChanged);
-
- public static Color GetTintColor(BindableObject element)
- {
- return (Color)element.GetValue(TintColorProperty);
- }
-
- public static void SetTintColor(BindableObject element, Color value)
- {
- element.SetValue(TintColorProperty, value);
- }
-
-
- static void OnTintColorPropertyPropertyChanged(BindableObject bindable, object oldValue, object newValue)
- {
- AttachEffect(bindable as Image, (Color)newValue);
- }
-
- static void AttachEffect(Image element, Color color)
- {
- var effect = element.Effects.FirstOrDefault(x => x is TintImage) as TintImage;
-
- if (effect != null)
- {
- element.Effects.Remove(effect);
- }
-
- element.Effects.Add(new TintImage(color));
- }
-
- }
Android Implementation
Here, implement Platform Effects for TintImage at Runtime.
TintImageImpl.cs
- [assembly: ExportEffect(typeof(TintImageImpl), nameof(XamarinStudy.Common.Controls.TintImage))]
- namespace XamarinStudy.Droid.Effects
- {
- public class TintImageImpl: PlatformEffect
- {
- protected override void OnAttached()
- {
- try
- {
- var effect = (XamarinStudy.Common.Controls.TintImage)Element.Effects.FirstOrDefault(e => e is XamarinStudy.Common.Controls.TintImage);
-
- if (effect == null || !(Control is ImageView image))
- return;
-
- var filter = new PorterDuffColorFilter(effect.TintColor.ToAndroid(), PorterDuff.Mode.SrcIn);
- image.SetColorFilter(filter);
- }
- catch (Exception ex)
- {
-
- }
- }
-
- protected override void OnDetached() { }
- }
- }
iOS Implementation
Here, implement Platform Effects for TintImage at Runtime.
TintImageImpl.cs
- [assembly: ExportEffect(typeof(XamarinStudy.iOS.Effects.TintImageImpl), nameof(TintImage))]
- namespace XamarinStudy.iOS.Effects
- {
- public class TintImageImpl: PlatformEffect
- {
- protected override void OnAttached()
- {
- try
- {
- var effect = (XamarinStudy.Common.Controls.TintImage)Element.Effects.FirstOrDefault(e => e is XamarinStudy.Common.Controls.TintImage);
-
- if (effect == null || !(Control is UIImageView image))
- return;
-
- image.Image = image.Image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate);
- image.TintColor = effect.TintColor.ToUIColor();
- }
- catch (Exception ex)
- {
- }
- }
-
- protected override void OnDetached() { }
- }
- }
Create ViewModel
Now, create a Property for binding the color to View. Then, create a ChangeColorCommand for changing the image color at runtime.
MainPageViewModel.cs
- public class MainPageViewModel:BaseViewModel
- {
- #region Fields
-
- private Command _changeColorCommand;
-
- private Color _imageColor;
- #endregion
-
- #region Constructor
-
- public MainPageViewModel()
- {
- _imageColor = Color.Gray;
- }
-
- #endregion
-
- #region Properties
-
- public Color ImageColor
- {
- get { return _imageColor; }
- set { Set(() => ImageColor, ref _imageColor, value); }
- }
-
- public Command ChangeColorCommand
- {
- get
- {
- return _changeColorCommand ?? (_changeColorCommand = new Command(() =>
- {
- if(this.ImageColor==Color.Black)
- {
- this.ImageColor = Color.Gray;
- }
- else
- {
- this.ImageColor = Color.Black;
- }
- }));
- }
- }
-
- #endregion
-
- }
Consuming the TintImage
Now, use the TintImage effect in your XAML code.
MainPage.xaml
- <?xml version="1.0" encoding="utf-8" ?>
- <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
- xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
- xmlns:d="http://xamarin.com/schemas/2014/forms/design"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- mc:Ignorable="d"
- xmlns:behaviors="clr-namespace:XamarinStudy.Common.Behaviors"
- xmlns:converters="clr-namespace:XamarinStudy.Common.Converters"
- xmlns:Effects="clr-namespace:XamarinStudy.Common.Controls"
- x:Class="XamarinStudy.MainPage" Title="Effects">
-
- <StackLayout Margin="10,30,10,0" Spacing="20">
- <Image Source="banner.png"/>
- <Image Source="favicon.png" Effects:TintImageEffect.TintColor="{Binding ImageColor}">
- </Image>
-
- </StackLayout>
-
- </ContentPage>
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 :)