diff options
Diffstat (limited to 'Xamarin.Forms.Platform.Android/Renderers/SwitchRenderer.cs')
-rw-r--r-- | Xamarin.Forms.Platform.Android/Renderers/SwitchRenderer.cs | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/Xamarin.Forms.Platform.Android/Renderers/SwitchRenderer.cs b/Xamarin.Forms.Platform.Android/Renderers/SwitchRenderer.cs new file mode 100644 index 00000000..39916b58 --- /dev/null +++ b/Xamarin.Forms.Platform.Android/Renderers/SwitchRenderer.cs @@ -0,0 +1,83 @@ +using System; +using Android.Widget; +using ASwitch = Android.Widget.Switch; + +namespace Xamarin.Forms.Platform.Android +{ + public class SwitchRenderer : ViewRenderer<Switch, ASwitch>, CompoundButton.IOnCheckedChangeListener + { + public SwitchRenderer() + { + AutoPackage = false; + } + + void CompoundButton.IOnCheckedChangeListener.OnCheckedChanged(CompoundButton buttonView, bool isChecked) + { + ((IViewController)Element).SetValueFromRenderer(Switch.IsToggledProperty, isChecked); + } + + public override SizeRequest GetDesiredSize(int widthConstraint, int heightConstraint) + { + SizeRequest sizeConstraint = base.GetDesiredSize(widthConstraint, heightConstraint); + + if (sizeConstraint.Request.Width == 0) + { + int width = widthConstraint; + if (widthConstraint <= 0) + width = (int)Context.GetThemeAttributeDp(global::Android.Resource.Attribute.SwitchMinWidth); + else if (widthConstraint <= 0) + width = 100; + + sizeConstraint = new SizeRequest(new Size(width, sizeConstraint.Request.Height), new Size(width, sizeConstraint.Minimum.Height)); + } + + return sizeConstraint; + } + + protected override void Dispose(bool disposing) + { + if (disposing && Control != null) + { + if (Element != null) + Element.Toggled -= HandleToggled; + + Control.SetOnCheckedChangeListener(null); + } + + base.Dispose(disposing); + } + + protected override void OnElementChanged(ElementChangedEventArgs<Switch> e) + { + base.OnElementChanged(e); + + if (e.OldElement != null) + e.OldElement.Toggled -= HandleToggled; + + if (e.NewElement != null) + { + if (Control == null) + { + var aswitch = new ASwitch(Context); + aswitch.SetOnCheckedChangeListener(this); + SetNativeControl(aswitch); + } + else + UpdateEnabled(); // Normally set by SetNativeControl, but not when the Control is reused. + + e.NewElement.Toggled += HandleToggled; + Control.Checked = e.NewElement.IsToggled; + } + } + + void HandleToggled(object sender, EventArgs e) + { + Control.Checked = Element.IsToggled; + } + + void UpdateEnabled() + { + Control.Enabled = Element.IsEnabled; + } + } +}
\ No newline at end of file |