Issue
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:
Using the code suggested below:
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);
}
}
}
Solution
Don't set the native control, instead just update the current Control, like this:
this.Control.ThumbTintColor = s.SwitchThumbColor.ToUIColor();
this.Control.OnTintColor = s.SwitchOnColor.ToUIColor();
And theres no need to call SetNativeControl
Answered By - sme
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.