I created a switch with a label related to it, in the sense that when Switch in turned on/off the label change it's text. Then I created a custom switch render who work very well for me, but I have an inconvenience: with the render my switch continue to work, but label not change.
I attach the code below.
Switch Toggle Event:
private void OnSwitchToggled(object sender, EventArgs e) {
if (swcSave.IsToggled) {
lblSave.Text = "text1";
} else {
lblSave.Text = "text2";
}
}
Switch Custom Render:
using Xamarin.Forms.Platform.Android;
using Android.Widget;
using Android.Graphics;
using MyApp.Droid;
[assembly: Xamarin.Forms.ExportRenderer(typeof(Xamarin.Forms.Switch), typeof(SwitchRenderer_Android))]
namespace MyApp.Droid
{
class SwitchRenderer_Android : SwitchRenderer
{
private Color blue = new Color(56, 103, 150);
private Color yellow = new Color(243, 146, 0);
protected override void Dispose(bool disposing)
{
Control.CheckedChange += this.OnCheckedChange;
base.Dispose(disposing);
}
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Switch> e)
{
base.OnElementChanged(e);
if (this.Control != null) {
if (this.Control.Checked) {
this.Control.ThumbDrawable.SetColorFilter(yellow, PorterDuff.Mode.SrcAtop);
this.Control.TrackDrawable.SetColorFilter(yellow, PorterDuff.Mode.SrcAtop);
} else {
this.Control.ThumbDrawable.SetColorFilter(blue, PorterDuff.Mode.SrcAtop);
this.Control.TrackDrawable.SetColorFilter(blue, PorterDuff.Mode.SrcAtop);
}
this.Control.CheckedChange += this.OnCheckedChange;
}
}
private void OnCheckedChange(object sender, CompoundButton.CheckedChangeEventArgs e)
{
if (this.Control.Checked)
{
this.Control.ThumbDrawable.SetColorFilter(yellow, PorterDuff.Mode.SrcAtop);
this.Control.TrackDrawable.SetColorFilter(yellow, PorterDuff.Mode.SrcAtop);
}
else
{
this.Control.ThumbDrawable.SetColorFilter(blue, PorterDuff.Mode.SrcAtop);
this.Control.TrackDrawable.SetColorFilter(blue, PorterDuff.Mode.SrcAtop);
}
}
}
}