diff --git a/Ooui.Forms/Exports.cs b/Ooui.Forms/Exports.cs index 1904bdd..3d16c15 100644 --- a/Ooui.Forms/Exports.cs +++ b/Ooui.Forms/Exports.cs @@ -17,6 +17,7 @@ using Xamarin.Forms; [assembly: ExportRenderer (typeof (LinkView), typeof (LinkViewRenderer))] [assembly: ExportRenderer (typeof (ProgressBar), typeof (ProgressBarRenderer))] [assembly: ExportRenderer (typeof (SearchBar), typeof (SearchBarRenderer))] +[assembly: ExportRenderer(typeof(Slider), typeof(SliderRenderer))] [assembly: ExportRenderer (typeof (Switch), typeof (SwitchRenderer))] [assembly: ExportRenderer (typeof (TimePicker), typeof (TimePickerRenderer))] [assembly: ExportImageSourceHandler (typeof (FileImageSource), typeof (FileImageSourceHandler))] diff --git a/Ooui.Forms/Renderers/SliderRenderer.cs b/Ooui.Forms/Renderers/SliderRenderer.cs new file mode 100644 index 0000000..d223613 --- /dev/null +++ b/Ooui.Forms/Renderers/SliderRenderer.cs @@ -0,0 +1,93 @@ +using System.ComponentModel; +using Xamarin.Forms; + +namespace Ooui.Forms.Renderers +{ + public class SliderRenderer : ViewRenderer + { + private bool _disposed; + + IElementController ElementController => Element as IElementController; + + public override SizeRequest GetDesiredSize(double widthConstraint, double heightConstraint) + { + var size = new Size(100, 38); + return new SizeRequest(size, size); + } + + protected override void OnElementChanged(ElementChangedEventArgs e) + { + base.OnElementChanged(e); + + if (e.NewElement == null) + return; + + if (Control == null) + { + var range = new Input + { + ClassName = "form-control", + Type = InputType.Range + }; + + range.Change += OnValueChange; + + SetNativeControl(range); + } + + UpdateMaximum(); + UpdateMinimum(); + UpdateValue(); + } + + protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) + { + base.OnElementPropertyChanged(sender, e); + + if (e.PropertyName == Slider.MaximumProperty.PropertyName) + UpdateMaximum(); + else if (e.PropertyName == Slider.MinimumProperty.PropertyName) + UpdateMinimum(); + else if (e.PropertyName == Slider.ValueProperty.PropertyName) + UpdateValue(); + } + + protected override void Dispose(bool disposing) + { + if (_disposed) + return; + + _disposed = true; + + if (disposing) + { + if (Control != null) + { + Control.Change -= OnValueChange; + } + } + + base.Dispose(disposing); + } + + private void UpdateMaximum() + { + Control.Maximum = Element.Maximum; + } + + private void UpdateMinimum() + { + Control.Minimum = Element.Minimum; + } + + private void UpdateValue() + { + Control.NumberValue = Element.Value; + } + + private void OnValueChange(object sender, TargetEventArgs e) + { + ElementController.SetValueFromRenderer(Slider.ValueProperty, Control.NumberValue); + } + } +} diff --git a/Samples/Program.cs b/Samples/Program.cs index dc9166d..1c7466d 100644 --- a/Samples/Program.cs +++ b/Samples/Program.cs @@ -33,6 +33,7 @@ namespace Samples new DotMatrixClockSample().Publish(); new EditorSample().Publish(); new SearchBarSample().Publish(); + new SliderSample().Publish(); new TimePickerSample().Publish(); new TipCalcSample().Publish(); new WeatherAppSample().Publish(); diff --git a/Samples/SliderSample.cs b/Samples/SliderSample.cs new file mode 100644 index 0000000..8b858d0 --- /dev/null +++ b/Samples/SliderSample.cs @@ -0,0 +1,59 @@ +using Ooui; +using System; +using Xamarin.Forms; + +namespace Samples +{ + public class SliderSample : ISample + { + private Xamarin.Forms.Label _label; + + public string Title => "Xamarin.Forms Slider Sample"; + + public Ooui.Element CreateElement() + { + var panel = new StackLayout(); + + var titleLabel = new Xamarin.Forms.Label + { + Text = "Slider", + FontSize = 24, + FontAttributes = FontAttributes.Bold + }; + panel.Children.Add(titleLabel); + + Slider slider = new Slider + { + Minimum = 0, + Maximum = 100 + }; + panel.Children.Add(slider); + + slider.ValueChanged += OnSliderValueChanged; + + _label = new Xamarin.Forms.Label + { + Text = "Slider value is 0", + HorizontalOptions = LayoutOptions.Center + }; + panel.Children.Add(_label); + + var page = new ContentPage + { + Content = panel + }; + + return page.GetOouiElement(); + } + + void OnSliderValueChanged(object sender, ValueChangedEventArgs e) + { + _label.Text = String.Format("Slider value is {0:F1}", e.NewValue); + } + + public void Publish() + { + UI.Publish("/slider", CreateElement); + } + } +}