Skip to content
36 changes: 29 additions & 7 deletions src/Compatibility/Core/src/Android/Renderers/DatePickerRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,11 @@ void IPickerRenderer.OnClick()
DatePicker view = Element;
((IElementController)view).SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, true);

ShowPickerDialog(view.Date.Year, view.Date.Month - 1, view.Date.Day);
var year = view.Date?.Year ?? DateTime.Today.Year;
var month = (view.Date?.Month ?? DateTime.Today.Month) - 1;
var day = view.Date?.Day ?? DateTime.Today.Day;

ShowPickerDialog(year, month, day);
}

void ShowPickerDialog(int year, int month, int day)
Expand Down Expand Up @@ -164,19 +168,19 @@ void OnCancelButtonClicked(object sender, EventArgs e)
Element.Unfocus();
}

void SetDate(DateTime date)
void SetDate(DateTime? date)
{
if (String.IsNullOrWhiteSpace(Element.Format))
{
EditText.Text = date.ToShortDateString();
EditText.Text = date?.ToShortDateString();
}
else if (Element.Format.Contains('/', StringComparison.Ordinal))
{
EditText.Text = date.ToString(Element.Format, CultureInfo.InvariantCulture);
EditText.Text = date?.ToString(Element.Format, CultureInfo.InvariantCulture);
}
else
{
EditText.Text = date.ToString(Element.Format);
EditText.Text = date?.ToString(Element.Format);
}
}

Expand All @@ -198,7 +202,16 @@ void UpdateMaximumDate()
{
if (_dialog != null)
{
_dialog.DatePicker.MaxDate = (long)Element.MaximumDate.ToUniversalTime().Subtract(DateTime.MinValue.AddYears(1969)).TotalMilliseconds;
if (Element.MaximumDate is null)
{
_dialog.DatePicker.MaxDate = (long)DateTime.MaxValue.ToUniversalTime()
.Subtract(DateTime.MinValue.AddYears(1969)).TotalMilliseconds;

return;
}

_dialog.DatePicker.MaxDate = (long)Element.MaximumDate.Value
.ToUniversalTime().Subtract(DateTime.MinValue.AddYears(1969)).TotalMilliseconds;
}
}

Expand All @@ -207,7 +220,16 @@ void UpdateMinimumDate()
{
if (_dialog != null)
{
_dialog.DatePicker.MinDate = (long)Element.MinimumDate.ToUniversalTime().Subtract(DateTime.MinValue.AddYears(1969)).TotalMilliseconds;
if (Element.MinimumDate is null)
{
_dialog.DatePicker.MinDate = (long)DateTime.MinValue.ToUniversalTime()
.Subtract(DateTime.MinValue.AddYears(1969)).TotalMilliseconds;

return;
}

_dialog.DatePicker.MinDate = (long)Element.MinimumDate.Value
.ToUniversalTime().Subtract(DateTime.MinValue.AddYears(1969)).TotalMilliseconds;
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/Compatibility/Core/src/Windows/DatePickerRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ void OnControlDateChanged(object sender, DatePickerValueChangedEventArgs e)
if (Element == null)
return;

if (Element.Date.CompareTo(e.NewDate.Date) != 0)
if (Element.Date == null || Element.Date?.CompareTo(e.NewDate.Date) != 0)
{
var date = e.NewDate.Date.Clamp(Element.MinimumDate, Element.MaximumDate);
var date = e.NewDate.Date.Clamp(Element.MinimumDate ?? DateTime.MinValue, Element.MaximumDate ?? DateTime.MaxValue);
Element.Date = date;

// set the control date-time to clamped value, if it exceeded the limits at the time of installation.
Expand All @@ -148,10 +148,10 @@ bool CheckDateFormat()
}

[PortHandler]
void UpdateDate(DateTime date)
void UpdateDate(DateTime? date)
{
if (Control != null)
Control.Date = new DateTimeOffset(new DateTime(date.Ticks, DateTimeKind.Unspecified));
Control.Date = new DateTimeOffset(new DateTime(date?.Ticks ?? 0, DateTimeKind.Unspecified));

UpdateDay();
UpdateMonth();
Expand Down Expand Up @@ -288,18 +288,18 @@ void UpdateFont()
void UpdateMaximumDate()
{
if (Element != null && Control != null)
Control.MaxYear = new DateTimeOffset(new DateTime(Element.MaximumDate.Ticks, DateTimeKind.Unspecified));
Control.MaxYear = new DateTimeOffset(new DateTime(Element.MaximumDate?.Ticks ?? DateTime.MaxValue.Ticks, DateTimeKind.Unspecified));
}

[PortHandler]
void UpdateMinimumDate()
{
DateTime mindate = Element.MinimumDate;
DateTime mindate = Element.MinimumDate ?? DateTime.MinValue;

try
{
if (Element != null && Control != null)
Control.MinYear = new DateTimeOffset(new DateTime(Element.MinimumDate.Ticks, DateTimeKind.Unspecified));
Control.MinYear = new DateTimeOffset(new DateTime(Element.MinimumDate?.Ticks ?? DateTime.MinValue.Ticks, DateTimeKind.Unspecified));
}
catch (ArgumentOutOfRangeException)
{
Expand Down
12 changes: 6 additions & 6 deletions src/Compatibility/Core/src/iOS/Renderers/DatePickerRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ void OnStarted(object sender, EventArgs eventArgs)
[PortHandler]
void UpdateDateFromModel(bool animate)
{
if (_picker.Date.ToDateTime().Date != Element.Date.Date)
_picker.SetDate(Element.Date.ToNSDate(), animate);
if (_picker.Date.ToDateTime().Date != Element.Date?.Date)
_picker.SetDate(Element.Date?.ToNSDate(), animate);

// Can't use Element.Format because it won't display the correct format if the region and language are set differently
if (string.IsNullOrWhiteSpace(Element.Format) || Element.Format.Equals("d", StringComparison.OrdinalIgnoreCase))
Expand All @@ -200,11 +200,11 @@ void UpdateDateFromModel(bool animate)
}
else if (Element.Format.Contains('/', StringComparison.Ordinal))
{
Control.Text = Element.Date.ToString(Element.Format, CultureInfo.InvariantCulture);
Control.Text = Element.Date?.ToString(Element.Format, CultureInfo.InvariantCulture);
}
else
{
Control.Text = Element.Date.ToString(Element.Format);
Control.Text = Element.Date?.ToString(Element.Format);
}
}

Expand Down Expand Up @@ -237,13 +237,13 @@ void UpdateCharacterSpacing()
[PortHandler]
void UpdateMaximumDate()
{
_picker.MaximumDate = Element.MaximumDate.ToNSDate();
_picker.MaximumDate = Element.MaximumDate?.ToNSDate();
}

[PortHandler]
void UpdateMinimumDate()
{
_picker.MinimumDate = Element.MinimumDate.ToNSDate();
_picker.MinimumDate = Element.MinimumDate?.ToNSDate();
}

[PortHandler]
Expand Down
101 changes: 0 additions & 101 deletions src/Controls/docs/Microsoft.Maui.Controls/DateChangedEventArgs.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@
Margin="6, 0"
Style="{StaticResource IsFocusedTextStyle}"/>
</HorizontalStackLayout>
</VerticalStackLayout>
<Label
Text="Set to null"
Style="{StaticResource Headline}"/>
<DatePicker
x:Name="NullDatePicker"
Date="{x:Null}"/>
<Button Text="Set to null" Clicked="SetDatePickerToNull" />
<Button Text="Set to today" Clicked="SetDatePickerToToday" />
</VerticalStackLayout>
</views:BasePage.Content>
</views:BasePage>
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ public DatePickerPage()
UpdateDatePickerBackground();
}

void OnUpdateBackgroundButtonClicked(object sender, System.EventArgs e)
void OnUpdateBackgroundButtonClicked(object sender, EventArgs e)
{
UpdateDatePickerBackground();
}

void OnClearBackgroundButtonClicked(object sender, System.EventArgs e)
void OnClearBackgroundButtonClicked(object sender, EventArgs e)
{
BackgroundDatePicker.Background = null;
}
Expand All @@ -41,14 +41,24 @@ void UpdateDatePickerBackground()
};
}

void OnFocusDatePickerFocused(object sender, Microsoft.Maui.Controls.FocusEventArgs e)
void OnFocusDatePickerFocused(object sender, FocusEventArgs e)
{
Debug.WriteLine("Focused");
}

void OnFocusDatePickerUnfocused(object sender, Microsoft.Maui.Controls.FocusEventArgs e)
void OnFocusDatePickerUnfocused(object sender, FocusEventArgs e)
{
Debug.WriteLine("Unfocused");
}
}

void SetDatePickerToNull(object sender, EventArgs e)
{
NullDatePicker.Date = null;
}

void SetDatePickerToToday(object sender, EventArgs e)
{
NullDatePicker.Date = DateTime.Now;
}
}
}
38 changes: 23 additions & 15 deletions src/Controls/src/Core/DateChangedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
#nullable disable
using System;

namespace Microsoft.Maui.Controls
namespace Microsoft.Maui.Controls;

/// <summary>
/// Event arguments for <see cref="DatePicker.DateSelected" /> event.
/// </summary>
public class DateChangedEventArgs : EventArgs
{
/// <include file="../../docs/Microsoft.Maui.Controls/DateChangedEventArgs.xml" path="Type[@FullName='Microsoft.Maui.Controls.DateChangedEventArgs']/Docs/*" />
public class DateChangedEventArgs : EventArgs
/// <summary>
/// Creates a new <see cref="DateChangedEventArgs" /> object that represents a change from <paramref name="oldDate" /> to <paramref name="newDate" />
/// </summary>
/// <param name="oldDate">The old date value.</param>
/// <param name="newDate">The new date value.</param>
public DateChangedEventArgs(DateTime? oldDate, DateTime? newDate)
{
/// <include file="../../docs/Microsoft.Maui.Controls/DateChangedEventArgs.xml" path="//Member[@MemberName='.ctor']/Docs/*" />
public DateChangedEventArgs(DateTime oldDate, DateTime newDate)
{
OldDate = oldDate;
NewDate = newDate;
}
OldDate = oldDate;
NewDate = newDate;
}

/// <include file="../../docs/Microsoft.Maui.Controls/DateChangedEventArgs.xml" path="//Member[@MemberName='NewDate']/Docs/*" />
public DateTime NewDate { get; private set; }
/// <summary>
/// The date that the user entered.
/// </summary>
public DateTime? NewDate { get; private set; }

/// <include file="../../docs/Microsoft.Maui.Controls/DateChangedEventArgs.xml" path="//Member[@MemberName='OldDate']/Docs/*" />
public DateTime OldDate { get; private set; }
}
/// <summary>
/// The date that was on the element at the time that the user selected it.
/// </summary>
public DateTime? OldDate { get; private set; }
}
Loading
Loading