Added SliderRenderer and Slider Sample

This commit is contained in:
Javier Suárez Ruiz 2017-12-29 12:55:47 +01:00
parent 1bd07e7cfa
commit f12708d314
4 changed files with 154 additions and 0 deletions

View File

@ -15,6 +15,7 @@ using Xamarin.Forms;
[assembly: ExportRenderer(typeof(Label), typeof(LabelRenderer))]
[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))]

View File

@ -0,0 +1,93 @@
using System.ComponentModel;
using Xamarin.Forms;
namespace Ooui.Forms.Renderers
{
public class SliderRenderer : ViewRenderer<Slider, Input>
{
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<Slider> 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);
}
}
}

View File

@ -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();

59
Samples/SliderSample.cs Normal file
View File

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