Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Switch Toggled event handler not triggering

jagdeeshkjagdeeshk Member ✭✭
edited May 26 in Xamarin.Forms

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 )

Best Answer

Answers

  • jagdeeshkjagdeeshk Member ✭✭
    edited May 28

    @Jarvan Thank you it's working fine now. I missed this line of code
    Element.IsToggled = Control.Checked;

Sign In or Register to comment.