I my project created switch with Toggled event handler its works fine default but When I added custom render to switch Toggled event handler not working .
Xaml<local:CustomSwitch Toggled="Switch_Toggled" />
Custom Render Android ( CustomSwitchRenderer.cs )
[assembly: ExportRenderer(typeof(CustomSwitch), typeof(CustomSwitchRenderer))] namespace Project.Droid { public class CustomSwitchRenderer : SwitchRenderer { public CustomSwitchRenderer(Context context) : base(context) { } private Android.Graphics.Color greyColor = new Android.Graphics.Color(189, 189, 189); private Android.Graphics.Color greenColor = new Android.Graphics.Color(32, 156, 68); protected override void Dispose(bool disposing) { this.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.TrackDrawable.SetColorFilter(greenColor, PorterDuff.Mode.SrcAtop); this.Control.ThumbDrawable.SetColorFilter(greenColor, PorterDuff.Mode.SrcAtop); } else { this.Control.TrackDrawable.SetColorFilter(greyColor, PorterDuff.Mode.SrcAtop); this.Control.ThumbDrawable.SetColorFilter(greyColor, PorterDuff.Mode.SrcAtop); } this.Control.CheckedChange += this.OnCheckedChange; } } private void OnCheckedChange(object sender, CompoundButton.CheckedChangeEventArgs e) { if (this.Control.Checked) { this.Control.TrackDrawable.SetColorFilter(greenColor, PorterDuff.Mode.SrcAtop); this.Control.ThumbDrawable.SetColorFilter(greenColor, PorterDuff.Mode.SrcAtop); } else { this.Control.TrackDrawable.SetColorFilter(greyColor, PorterDuff.Mode.SrcAtop); this.Control.ThumbDrawable.SetColorFilter(greyColor, PorterDuff.Mode.SrcAtop); } } } }
Shared project file ( CustomSwitch.cs )
namespace Project { public class CustomSwitch : Switch { } }
Is there any mistake in custom render ? ( Without custom render working fine )
Switch Toggled event handler not triggering
@jagdeeshk I created a basic demo to test the code, the color style works fine which indicates that the status of the Checked property is correct. So you could try to set the Checked's value to the IsToggled property.
[assembly : ExportRenderer(typeof(CustomSwitch), typeof(CustomSwitchRenderer))] namespace App19F_5.Droid { public class CustomSwitchRenderer : SwitchRenderer { ... private void OnCheckedChange(object sender, CompoundButton.CheckedChangeEventArgs e) { if (this.Control.Checked) { this.Control.TrackDrawable.SetColorFilter(greenColor, PorterDuff.Mode.SrcAtop); this.Control.ThumbDrawable.SetColorFilter(greenColor, PorterDuff.Mode.SrcAtop); } else { this.Control.TrackDrawable.SetColorFilter(greyColor, PorterDuff.Mode.SrcAtop); this.Control.ThumbDrawable.SetColorFilter(greyColor, PorterDuff.Mode.SrcAtop); } Element.IsToggled = Control.Checked; } } }
Similar issue: https://stackoverflow.com/a/53036789/11083277
Answers
@jagdeeshk I created a basic demo to test the code, the color style works fine which indicates that the status of the Checked property is correct. So you could try to set the Checked's value to the IsToggled property.
Similar issue: https://stackoverflow.com/a/53036789/11083277
@Jarvan Thank you it's working fine now. I missed this line of code
Element.IsToggled = Control.Checked;