Merge pull request #46 from jsuarezruiz/master
Added TimePickerRenderer and TimePicker Sample
This commit is contained in:
commit
21ce458f45
|
@ -2,7 +2,6 @@
|
||||||
using Ooui.Forms;
|
using Ooui.Forms;
|
||||||
using Ooui.Forms.Renderers;
|
using Ooui.Forms.Renderers;
|
||||||
using Xamarin.Forms;
|
using Xamarin.Forms;
|
||||||
using Xamarin.Forms.Internals;
|
|
||||||
|
|
||||||
[assembly: Dependency(typeof(ResourcesProvider))]
|
[assembly: Dependency(typeof(ResourcesProvider))]
|
||||||
[assembly: ExportRenderer(typeof(ActivityIndicator), typeof(ActivityIndicatorRenderer))]
|
[assembly: ExportRenderer(typeof(ActivityIndicator), typeof(ActivityIndicatorRenderer))]
|
||||||
|
@ -15,6 +14,7 @@ using Xamarin.Forms.Internals;
|
||||||
[assembly: ExportRenderer(typeof(Image), typeof(ImageRenderer))]
|
[assembly: ExportRenderer(typeof(Image), typeof(ImageRenderer))]
|
||||||
[assembly: ExportRenderer(typeof(Label), typeof(LabelRenderer))]
|
[assembly: ExportRenderer(typeof(Label), typeof(LabelRenderer))]
|
||||||
[assembly: ExportRenderer(typeof(ProgressBar), typeof(ProgressBarRenderer))]
|
[assembly: ExportRenderer(typeof(ProgressBar), typeof(ProgressBarRenderer))]
|
||||||
|
[assembly: ExportRenderer(typeof(TimePicker), typeof(TimePickerRenderer))]
|
||||||
[assembly: ExportRenderer(typeof(Switch), typeof(SwitchRenderer))]
|
[assembly: ExportRenderer(typeof(Switch), typeof(SwitchRenderer))]
|
||||||
[assembly: ExportImageSourceHandler(typeof(FileImageSource), typeof(FileImageSourceHandler))]
|
[assembly: ExportImageSourceHandler(typeof(FileImageSource), typeof(FileImageSourceHandler))]
|
||||||
[assembly: ExportImageSourceHandler(typeof(StreamImageSource), typeof(StreamImagesourceHandler))]
|
[assembly: ExportImageSourceHandler(typeof(StreamImageSource), typeof(StreamImagesourceHandler))]
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -32,6 +32,7 @@ namespace Samples
|
||||||
new DisplayAlertSample ().Publish ();
|
new DisplayAlertSample ().Publish ();
|
||||||
new DotMatrixClockSample().Publish();
|
new DotMatrixClockSample().Publish();
|
||||||
new EditorSample().Publish();
|
new EditorSample().Publish();
|
||||||
|
new TimePickerSample().Publish();
|
||||||
new TipCalcSample().Publish();
|
new TipCalcSample().Publish();
|
||||||
new WeatherAppSample().Publish();
|
new WeatherAppSample().Publish();
|
||||||
new XuzzleSample().Publish();
|
new XuzzleSample().Publish();
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue