Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin Forms Switch's Toggled event not being triggered when using custom renderer

I have a Xamarin.Forms app and I have created a custom Switch as below:

public class ExtSwitch : Switch
{
   public static readonly BindableProperty SwitchOnColorProperty =
          BindableProperty.Create(nameof(SwitchOnColor),
                                  typeof(Color), typeof(ExtSwitch), Color.Default);

   public Color SwitchOnColor
   {
      get { return (Color)GetValue(SwitchOnColorProperty); }
      set { SetValue(SwitchOnColorProperty, value); }
   }

   // More codes here //
}

In My XAML I used it like:

<local:ExtSwitch Grid.Column = "2"
       IsToggled="{Binding IsToggled}" 
       Toggled="Handle_Toggled"
       SwitchThumbColor="White" 
       SwitchOnColor="Red" 
       SwitchOffColor="Gray"
       HorizontalOptions="End" 
       VerticalOptions="Center" />

In My C# code I have a Handle_Toggled method that handles what happened when the Swich is toggled. But somehow the Toggled event is not being triggered when used inside my custom switch but works perfectly when used in a normal switch.

Can someone point to me what am I missing here or what am I doing wrong?

Edit:

Custom renderer in iOS:

class ExtSwitchRenderer : SwitchRenderer
{
   protected override void OnElementChanged(ElementChangedEventArgs<Switch> e)
   {
       base.OnElementChanged(e);

       if (e.OldElement != null || e.NewElement == null) return;

       ExtSwitch s = Element as ExtSwitch;

       UISwitch sw = new UISwitch();
       sw.ThumbTintColor = s.SwitchThumbColor.ToUIColor();
       sw.OnTintColor = s.SwitchOnColor.ToUIColor();

       SetNativeControl(sw);
    }
}

Using the above code:

enter image description here

Using the code suggested below:

enter image description here

Custom renderer in Android:

public class ExtSwitchRenderer : SwitchRenderer
    {
        public ExtSwitchRenderer(Context context) : base(context) { }
        ExtSwitch s;

       protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Switch> e)
      {
        base.OnElementChanged(e);
        if (e.OldElement != null || e.NewElement == null)
            return;

        s = Element as ExtSwitch;
        if (this.Control != null)
        {
            if (this.Control.Checked)
            {
                this.Control.TrackDrawable.SetColorFilter(s.SwitchOnColor.ToAndroid(), PorterDuff.Mode.SrcAtop);
            }
            else
            {
                this.Control.TrackDrawable.SetColorFilter(s.SwitchOffColor.ToAndroid(), PorterDuff.Mode.SrcAtop);
            }
            this.Control.CheckedChange += this.OnCheckedChange;
        }
        Control.Toggle();
    }

    void OnCheckedChange(object sender, CompoundButton.CheckedChangeEventArgs e)
    {
        if (this.Control.Checked)
        {
            this.Control.ThumbDrawable.SetColorFilter(s.SwitchOnColor.ToAndroid(), PorterDuff.Mode.Multiply);
            this.Control.TrackDrawable.SetColorFilter(s.SwitchOnColor.ToAndroid(), PorterDuff.Mode.SrcAtop);
        }
        else
        {
            this.Control.ThumbDrawable.SetColorFilter(s.SwitchOffColor.ToAndroid(), PorterDuff.Mode.Multiply);
            this.Control.TrackDrawable.SetColorFilter(s.SwitchOffColor.ToAndroid(), PorterDuff.Mode.SrcAtop);
        }
    }
}
like image 817
iamsophia Avatar asked Mar 07 '23 14:03

iamsophia


1 Answers

on IOs renderer

protected override void OnElementChanged(ElementChangedEventArgs<Switch> e)
    {
        base.OnElementChanged(e);



        if (e.OldElement != null || e.NewElement == null) return;

        CustomSwitch s = Element as CustomSwitch;



        //this.Control.ThumbTintColor = s.SwitchThumbColor.ToUIColor();
        this.Control.OnTintColor = s.SwitchOnColor.ToUIColor();

    }

and Android renderer in OnCheckedChange

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);
        }

        Element.IsToggled = Control.Checked;


    }
like image 82
Geral Torres Avatar answered May 10 '23 19:05

Geral Torres