Merge pull request #46 from jsuarezruiz/master

Added TimePickerRenderer and TimePicker Sample
This commit is contained in:
Frank A. Krueger 2017-12-26 20:16:39 -06:00 committed by GitHub
commit 21ce458f45
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 148 additions and 16 deletions

View File

@ -2,23 +2,23 @@
using Ooui.Forms;
using Ooui.Forms.Renderers;
using Xamarin.Forms;
using Xamarin.Forms.Internals;
[assembly: Dependency (typeof (ResourcesProvider))]
[assembly: ExportRenderer (typeof (ActivityIndicator), typeof (ActivityIndicatorRenderer))]
[assembly: ExportRenderer (typeof (BoxView), typeof (BoxRenderer))]
[assembly: ExportRenderer (typeof (Button), typeof (ButtonRenderer))]
[assembly: ExportRenderer (typeof (DatePicker), typeof (DatePickerRenderer))]
[assembly: ExportRenderer (typeof (Editor), typeof (EditorRenderer))]
[assembly: ExportRenderer (typeof (Entry), typeof (EntryRenderer))]
[assembly: ExportRenderer (typeof (Frame), typeof (FrameRenderer))]
[assembly: ExportRenderer (typeof (Image), typeof (ImageRenderer))]
[assembly: ExportRenderer (typeof (Label), typeof (LabelRenderer))]
[assembly: ExportRenderer (typeof (ProgressBar), typeof (ProgressBarRenderer))]
[assembly: ExportRenderer (typeof (Switch), typeof (SwitchRenderer))]
[assembly: ExportImageSourceHandler (typeof (FileImageSource), typeof (FileImageSourceHandler))]
[assembly: ExportImageSourceHandler (typeof (StreamImageSource), typeof (StreamImagesourceHandler))]
[assembly: ExportImageSourceHandler (typeof (UriImageSource), typeof (ImageLoaderSourceHandler))]
[assembly: Dependency(typeof(ResourcesProvider))]
[assembly: ExportRenderer(typeof(ActivityIndicator), typeof(ActivityIndicatorRenderer))]
[assembly: ExportRenderer(typeof(BoxView), typeof(BoxRenderer))]
[assembly: ExportRenderer(typeof(Button), typeof(ButtonRenderer))]
[assembly: ExportRenderer(typeof(DatePicker), typeof(DatePickerRenderer))]
[assembly: ExportRenderer(typeof(Editor), typeof(EditorRenderer))]
[assembly: ExportRenderer(typeof(Entry), typeof(EntryRenderer))]
[assembly: ExportRenderer(typeof(Frame), typeof(FrameRenderer))]
[assembly: ExportRenderer(typeof(Image), typeof(ImageRenderer))]
[assembly: ExportRenderer(typeof(Label), typeof(LabelRenderer))]
[assembly: ExportRenderer(typeof(ProgressBar), typeof(ProgressBarRenderer))]
[assembly: ExportRenderer(typeof(TimePicker), typeof(TimePickerRenderer))]
[assembly: ExportRenderer(typeof(Switch), typeof(SwitchRenderer))]
[assembly: ExportImageSourceHandler(typeof(FileImageSource), typeof(FileImageSourceHandler))]
[assembly: ExportImageSourceHandler(typeof(StreamImageSource), typeof(StreamImagesourceHandler))]
[assembly: ExportImageSourceHandler(typeof(UriImageSource), typeof(ImageLoaderSourceHandler))]
namespace Ooui.Forms
{

View File

@ -0,0 +1,92 @@
using System;
using System.ComponentModel;
using Xamarin.Forms;
using Ooui.Forms.Extensions;
namespace Ooui.Forms.Renderers
{
public class TimePickerRenderer : ViewRenderer<TimePicker, Input>
{
bool _disposed;
IElementController ElementController => Element as IElementController;
public override SizeRequest GetDesiredSize(double widthConstraint, double heightConstraint)
{
var size = "00:00:00".MeasureSize(string.Empty, 16.0, FontAttributes.None);
size = new Size(size.Width, size.Height * 1.428 + 14);
return new SizeRequest(size, size);
}
protected override void OnElementChanged(ElementChangedEventArgs<TimePicker> e)
{
base.OnElementChanged(e);
if (e.NewElement == null)
return;
if (Control == null)
{
var entry = new Input
{
ClassName = "form-control",
Type = InputType.Time
};
entry.Change += OnEnded;
SetNativeControl(entry);
}
UpdateTime(false);
UpdateTextColor();
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == TimePicker.TimeProperty.PropertyName || e.PropertyName == TimePicker.FormatProperty.PropertyName)
UpdateTime(true);
else if (e.PropertyName == TimePicker.TextColorProperty.PropertyName)
UpdateTextColor();
}
void OnEnded(object sender, EventArgs eventArgs)
{
TimeSpan.TryParse(Control.Value, out var time);
ElementController?.SetValueFromRenderer(TimePicker.TimeProperty, time);
ElementController?.SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, false);
}
void UpdateTime(bool animate)
{
Control.Value = Element.Time.ToString(@"hh\:mm\:ss");
}
void UpdateTextColor()
{
var textColor = (Xamarin.Forms.Color)Element.GetValue(TimePicker.TextColorProperty);
Control.Style.Color = textColor.ToOouiColor(Xamarin.Forms.Color.Black);
}
protected override void Dispose(bool disposing)
{
if (_disposed)
return;
_disposed = true;
if (disposing)
{
if (Control != null)
{
Control.Change -= OnEnded;
}
}
base.Dispose(disposing);
}
}
}

View File

@ -32,6 +32,7 @@ namespace Samples
new DisplayAlertSample ().Publish ();
new DotMatrixClockSample().Publish();
new EditorSample().Publish();
new TimePickerSample().Publish();
new TipCalcSample().Publish();
new WeatherAppSample().Publish();
new XuzzleSample().Publish();

View File

@ -0,0 +1,39 @@
using Ooui;
using Xamarin.Forms;
namespace Samples
{
public class TimePickerSample : ISample
{
public string Title => "Xamarin.Forms TimePicker Sample";
public Ooui.Element CreateElement()
{
var panel = new StackLayout();
var titleLabel = new Xamarin.Forms.Label
{
Text = "TimePicker",
FontSize = 24,
FontAttributes = FontAttributes.Bold
};
panel.Children.Add(titleLabel);
var timePicker = new TimePicker();
panel.Children.Add(timePicker);
var page = new ContentPage
{
Content = panel
};
return page.GetOouiElement();
}
public void Publish()
{
UI.Publish("/timepicker", CreateElement);
}
}
}