If I understand clear what you want , you may need custom renderer to achieve this effect.
First, define a CustomSwitch in PCL with color BindableProperty(with it you can set value and use on different platforms)
CustomSwitch
public class CustomSwitch : Switch
{
public static readonly BindableProperty SwitchOffColorProperty =
BindableProperty.Create(nameof(SwitchOffColor),
typeof(Color), typeof(CustomSwitch),
Color.Default);
public Color SwitchOffColor
{
get { return (Color)GetValue(SwitchOffColorProperty); }
set { SetValue(SwitchOffColorProperty, value); }
}
public static readonly BindableProperty SwitchOnColorProperty =
BindableProperty.Create(nameof(SwitchOnColor),
typeof(Color), typeof(CustomSwitch),
Color.Default);
public Color SwitchOnColor
{
get { return (Color)GetValue(SwitchOnColorProperty); }
set { SetValue(SwitchOnColorProperty, value); }
}
public static readonly BindableProperty SwitchThumbColorProperty =
BindableProperty.Create(nameof(SwitchThumbColor),
typeof(Color), typeof(CustomSwitch),
Color.Default);
public Color SwitchThumbColor
{
get { return (Color)GetValue(SwitchThumbColorProperty); }
set { SetValue(SwitchThumbColorProperty, value); }
}
public static readonly BindableProperty SwitchThumbImageProperty =
BindableProperty.Create(nameof(SwitchThumbImage),
typeof(string),
typeof(CustomSwitch),
string.Empty);
public string SwitchThumbImage
{
get { return (string)GetValue(SwitchThumbImageProperty); }
set { SetValue(SwitchThumbImageProperty, value); }
}
}
Android
[assembly: ExportRenderer(typeof(CustomSwitch), typeof(CustomSwitchRenderer))]
namespace FormsApp.Android
{
public class CustomSwitchRenderer : SwitchRenderer
{
private CustomSwitch view;
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Switch> e)
{
base.OnElementChanged(e);
if (e.OldElement != null || e.NewElement == null)
return;
view = (CustomSwitch)Element;
if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.JellyBean)
{
if (this.Control != null)
{
if (this.Control.Checked)
{
this.Control.TrackDrawable.SetColorFilter(view.SwitchOnColor.ToAndroid(), PorterDuff.Mode.SrcAtop);
}
else
{
this.Control.TrackDrawable.SetColorFilter(view.SwitchOffColor.ToAndroid(), PorterDuff.Mode.SrcAtop);
}
this.Control.CheckedChange += this.OnCheckedChange;
UpdateSwitchThumbImage(view);
}
//Control.TrackDrawable.SetColorFilter(view.SwitchBGColor.ToAndroid(), PorterDuff.Mode.Multiply);
}
}
private void UpdateSwitchThumbImage(CustomSwitch view)
{
if (!string.IsNullOrEmpty(view.SwitchThumbImage))
{
view.SwitchThumbImage = view.SwitchThumbImage.Replace(".jpg", "").Replace(".png", "");
int imgid = (int)typeof(Resource.Drawable).GetField(view.SwitchThumbImage).GetValue(null);
Control.SetThumbResource(Resource.Drawable.icon);
}
else
{
Control.ThumbDrawable.SetColorFilter(view.SwitchThumbColor.ToAndroid(), PorterDuff.Mode.Multiply);
// Control.SetTrackResource(Resource.Drawable.track);
}
}
private void OnCheckedChange(object sender, CompoundButton.CheckedChangeEventArgs e)
{
if (this.Control.Checked)
{
this.Control.TrackDrawable.SetColorFilter(view.SwitchOnColor.ToAndroid(), PorterDuff.Mode.SrcAtop);
}
else
{
this.Control.TrackDrawable.SetColorFilter(view.SwitchOffColor.ToAndroid(), PorterDuff.Mode.SrcAtop);
}
}
protected override void Dispose(bool disposing)
{
this.Control.CheckedChange -= this.OnCheckedChange;
base.Dispose(disposing);
}
}
}
iOS
[assembly: ExportRenderer(typeof(CustomSwitch), typeof(CustomSwitchRenderer))]
namespace FormsApp2.iOS
{
class CustomSwitchRenderer : SwitchRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Switch> e)
{
base.OnElementChanged(e);
if (e.OldElement != null || e.NewElement == null) return;
CustomSwitch s = Element as CustomSwitch;
UISwitch sw = new UISwitch();
sw.ThumbTintColor = s.SwitchThumbColor.ToUIColor();
sw.OnTintColor = s.SwitchOnColor.ToUIColor();
SetNativeControl(sw);
}
}
}
Usage in Portable
<local:CustomSwitch Margin="50" SwitchOnColor="Red"/>
Don't forget add xmlns(namespace) in Xmal
xmlns:local = "clr-namespace:YouAppName"