This can be done using Effects .
Code
I created an example application here: https://github.com/brminnick/CustomStepper
Using effects in 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"
x:Class="CustomStepper.StepperPage"
xmlns:local="clr-namespace:CustomStepper">
<ContentPage.Content>
<StackLayout
HorizontalOptions="Center"
VerticalOptions="Center">
<local:RedStepper/>
<Stepper
local:StepperColorEffect.Color="Red"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>
Stride color effect
using System.Linq;
using Xamarin.Forms;
namespace CustomStepper
{
public static class StepperColorEffect
{
public static readonly BindableProperty ColorProperty =
BindableProperty.CreateAttached(nameof(Color),
typeof(Color),
typeof(Stepper),
Color.Gray,
propertyChanged: OnStepperColorChanged);
public static Color GetColor(BindableObject view) => (Color)view.GetValue(ColorProperty);
public static void SetColor(BindableObject view, Color value) => view.SetValue(ColorProperty, value);
static void OnStepperColorChanged(BindableObject bindable, object oldValue, object newValue) => UpdateEffect(bindable);
static void UpdateEffect(BindableObject bindable)
{
switch (bindable)
{
case Stepper stepper:
RemoveEffect(stepper);
stepper.Effects.Add(new StepperColorRoutingEffect());
break;
}
}
static void RemoveEffect(Stepper entry)
{
var effectToRemoveList = entry.Effects.Where(e => e is StepperColorRoutingEffect).ToList();
foreach (var entryReturnTypeEffect in effectToRemoveList)
entry.Effects.Remove(entryReturnTypeEffect);
}
}
class StepperColorRoutingEffect : RoutingEffect
{
public StepperColorRoutingEffect() : base("CustomStepper.StepperColorEffect")
{
}
}
}
iOS PlatformEffect
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using CustomStepper.iOS;
[assembly: ResolutionGroupName("CustomStepper")]
[assembly: ExportEffect(typeof(StepperColorEffect), nameof(StepperColorEffect))]
namespace CustomStepper.iOS
{
public class StepperColorEffect : PlatformEffect
{
protected override void OnAttached()
{
if (Element is Stepper element && Control is UIStepper control)
control.TintColor = CustomStepper.StepperColorEffect.GetColor(element).ToUIColor();
}
protected override void OnDetached()
{
if (Element is Stepper element && Control is UIStepper control)
control.TintColor = UIColor.Blue;
}
}
}
Android PlatformEffect
using Android.Widget;
using Android.Graphics;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using CustomStepper.Droid;
[assembly: ResolutionGroupName("CustomStepper")]
[assembly: ExportEffect(typeof(StepperColorEffect), nameof(StepperColorEffect))]
namespace CustomStepper.Droid
{
public class StepperColorEffect : PlatformEffect
{
protected override void OnAttached()
{
if (Element is Stepper element && Control is LinearLayout control)
{
control.GetChildAt(0).Background.SetColorFilter(CustomStepper.StepperColorEffect.GetColor(element).ToAndroid(), PorterDuff.Mode.Multiply);
control.GetChildAt(1).Background.SetColorFilter(CustomStepper.StepperColorEffect.GetColor(element).ToAndroid(), PorterDuff.Mode.Multiply);
}
}
protected override void OnDetached()
{
if (Element is Stepper element && Control is LinearLayout control)
{
control.GetChildAt(0).Background.SetColorFilter(Xamarin.Forms.Color.Gray.ToAndroid(), PorterDuff.Mode.Multiply);
control.GetChildAt(1).Background.SetColorFilter(Xamarin.Forms.Color.Gray.ToAndroid(), PorterDuff.Mode.Multiply);
}
}
}
}
Screenshots
Android

IOS

source
share