Add DatePickerRenderer

This commit is contained in:
Frank A. Krueger 2017-11-26 10:32:55 -08:00
parent b7d7bbfbb5
commit 6dc869b9e5
4 changed files with 116 additions and 3 deletions

View File

@ -8,6 +8,7 @@ using Xamarin.Forms.Internals;
[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 (Entry), typeof (EntryRenderer))]
[assembly: ExportRenderer (typeof (Label), typeof (LabelRenderer))]
[assembly: ExportRenderer (typeof (ProgressBar), typeof (ProgressBarRenderer))]

View File

@ -0,0 +1,111 @@
using System;
using System.ComponentModel;
using Xamarin.Forms;
using Ooui.Forms.Extensions;
namespace Ooui.Forms.Renderers
{
public class DatePickerRenderer : ViewRenderer<DatePicker, Input>
{
bool _disposed;
IElementController ElementController => Element as IElementController;
public override SizeRequest GetDesiredSize (double widthConstraint, double heightConstraint)
{
var size = "00/00/0000".MeasureSize ("", 16.0, FontAttributes.None);
size = new Size (size.Width, size.Height * 1.428 + 14);
return new SizeRequest (size, size);
}
protected override void OnElementChanged (ElementChangedEventArgs<DatePicker> e)
{
base.OnElementChanged (e);
if (e.NewElement == null)
return;
if (Control == null) {
var entry = new Input {
ClassName = "form-control",
Type = InputType.Date,
};
entry.Inputted += OnStarted;
entry.Changed += OnEnded;
SetNativeControl (entry);
}
UpdateDateFromModel (false);
UpdateMaximumDate ();
UpdateMinimumDate ();
UpdateTextColor ();
UpdateFlowDirection ();
}
protected override void OnElementPropertyChanged (object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged (sender, e);
if (e.PropertyName == DatePicker.DateProperty.PropertyName || e.PropertyName == DatePicker.FormatProperty.PropertyName)
UpdateDateFromModel (true);
else if (e.PropertyName == DatePicker.MinimumDateProperty.PropertyName)
UpdateMinimumDate ();
else if (e.PropertyName == DatePicker.MaximumDateProperty.PropertyName)
UpdateMaximumDate ();
else if (e.PropertyName == DatePicker.TextColorProperty.PropertyName || e.PropertyName == VisualElement.IsEnabledProperty.PropertyName)
UpdateTextColor ();
}
void OnEnded (object sender, EventArgs eventArgs)
{
DateTime.TryParseExact (Control.Value, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AssumeUniversal, out var date);
ElementController?.SetValueFromRenderer (DatePicker.DateProperty, date);
ElementController?.SetValueFromRenderer (VisualElement.IsFocusedPropertyKey, false);
}
void OnStarted (object sender, EventArgs eventArgs)
{
ElementController?.SetValueFromRenderer (VisualElement.IsFocusedPropertyKey, true);
}
void UpdateDateFromModel (bool animate)
{
Control.Value = Element.Date.ToString ("yyyy-MM-dd");
}
void UpdateFlowDirection ()
{
}
void UpdateMaximumDate ()
{
}
void UpdateMinimumDate ()
{
}
void UpdateTextColor ()
{
}
protected override void Dispose (bool disposing)
{
if (_disposed)
return;
_disposed = true;
if (disposing) {
if (Control != null) {
Control.Inputted -= OnStarted;
Control.Changed -= OnEnded;
}
}
base.Dispose (disposing);
}
}
}

View File

@ -4,10 +4,11 @@
x:Class="Samples.DisplayAlertPage">
<ContentPage.Content>
<StackLayout>
<Label Text="Welcome to DisplayAlert Sample!" />
<Label Text="Welcome to DisplayAlert Sample!" FontSize="32" FontAttributes="Bold" Margin="10,10,10,50" />
<ActivityIndicator x:Name="activity" />
<ProgressBar x:Name="progress" />
<Button Text="Tap for Display Alert"
<DatePicker x:Name="datePicker" />
<Button Text="Tap to Display Alert"
Clicked="OnButtonClicked" />
</StackLayout>
</ContentPage.Content>

View File

@ -18,7 +18,7 @@ namespace Samples
{
activity.IsRunning = true;
progress.Progress = 0.5;
var result = await DisplayAlert("Alert Message", "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper porta. Mauris massa.", "YES", "NO");
var result = await DisplayAlert($"Alert @ {datePicker.Date}", "This is a test of the dialog. Is it working?", "YES", "NO");
await DisplayAlert("Alert Response", $"You selected value: {result}", "OK");
activity.IsRunning = false;
progress.Progress = 1.0;