Reputation: 158
I'm using a date picker and time picker in my application.
And I implemented focus and unfocus events for date picker and time picker. Validating the time for the maximum date by using focus and unfocus events.
Those focus and unfocus events are triggering multiple times. Need to trigger once when we hit the date picker or time picker.
XAML:
<controls:DatePickerCustom x:Name="DatePicker" FontSize="14" HeightRequest="35" Date="{Binding CustomDate,Mode=TwoWay}" MaximumDate="{Binding MaximumDate}" FontFamily="Segoe UI" Format="D" HorizontalOptions="FillAndExpand" Unfocused="DatePickerUnfocused" TranslationX="-4">
<DatePicker.Triggers>
<DataTrigger TargetType="DatePicker" Binding="{Binding IsCustomSelected}" Value="true">
<Setter Property="TextColor" Value="{DynamicResource HeadingTextColor}" />
<Setter Property="TextColor" Value="{DynamicResource HeadingTextColor}" />
</DataTrigger>
</DatePicker.Triggers>
</controls:DatePickerCustom>
<controls:TimePickerCustom x:Name="TimePicker" FontSize="14" HeightRequest="35" Time="{Binding CustomTime,Mode=TwoWay}" FontFamily="Segoe UI" Focused="TimePickerFocused" Unfocused="TimePickerUnfocused" >
<TimePicker.Triggers>
<DataTrigger TargetType="TimePicker" Binding="{Binding IsCustomSelected}" Value="true">
<Setter Property="TextColor" Value="{DynamicResource HeadingTextColor}" />
</DataTrigger>
</TimePicker.Triggers>
</controls:TimePickerCustom>
CS:
protected void TimePickerUnfocused(object sender, FocusEventArgs e)
{
TimePicker picker = sender as TimePicker;
if (DatePicker.Date.ToString("dd/MM/yyyy") == DatePicker.MaximumDate.ToString("dd/MM/yyyy"))
{
//var t1 = Convert.ToDateTime(DateTime.Now).ToString("HH:mm");
//var t2 = Convert.ToDateTime(picker.Time.ToString()).ToString("HH:mm");
var result = DateTime.Compare(Convert.ToDateTime(Convert.ToDateTime(DateTime.Now).ToString("HH:mm")), Convert.ToDateTime(Convert.ToDateTime(picker.Time.ToString()).ToString("HH:mm")));
if (result > 0)
{
if (endValue.ToString() != "00:00:00")
{
TimePicker.Time = endValue;
}
else
{
endValue = picker.Time;
TimePicker.Time = endValue;
}
}
else
{
DisplayAlert("Alert!", "Please select the valid time", "OK");
TimePicker.Time = TimeSpan.Parse(DateTime.Now.AddHours(-1).ToString("HH:mm"));
}
}
}
protected void DatePickerUnfocused(object sender, FocusEventArgs e)
{
if (DatePicker.Date.ToString("dd/MM/yyyy HH:mm") == DatePicker.MaximumDate.ToString("dd/MM/yyyy HH:mm"))
{
if(endValue.ToString() != "00:00:00")
{
TimePicker.Time = endValue;
}
else if (startValue.ToString() != "00:00:00")
{
TimePicker.Time = startValue;
}
else
{
startValue= TimeSpan.Parse(DateTime.Now.AddHours(-1).ToString("HH:mm"));
TimePicker.Time = startValue;
}
}
}
protected void TimePickerFocused(object sender, FocusEventArgs e)
{
if (DatePicker.Date.ToString("dd/MM/yyyy HH:mm") == DatePicker.MaximumDate.ToString("dd/MM/yyyy HH:mm") && startValue != null)
{
TimePicker.Time = startValue;
}
}
And MinimumDate and MaximumDate attributes of the date picker are not working in the Android 10 version real device. same attributes are working in the emulator.
Upvotes: 1
Views: 1084
Reputation: 21213
Possible work-around:
Have a boolean that you set based on whether a specific picker is focused or not. Ignore any duplicate focus messages.
Example for DatePicker:
private bool _datePickerFocused = false;
protected void DatePickerFocused(...)
{
// Skip if already focused.
if (_datePickerFocused)
return;
_datePickerFocused = true;
...
}
protected void DatePickerUnfocused(...)
{
// Skip if not focused.
if (!_datePickerFocused)
return;
_datePickerFocused = false;
...
}
Upvotes: 0