Xamarin Forms:在 TimePicker 中关闭时钟弹出后,UI 上显示不同的时间?

Xamarin Forms: Different time is showing on the UI after clock pop is closed in TimePicker?

我正在使用下面的 在时间选择器中将时间设置为 5 的倍数。使用自定义渲染器,我能够 select 5 的倍数的时间。但是在 select 一段时间后,当时钟弹出窗口关闭时,[=24= 上显示不同的时间].问题仅在 android 平台中,对于 ios 一切正常。

我的代码:

public class CustomTimePickerRenderer : TimePickerRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.TimePicker> e)
    {
        base.OnElementChanged(e);

        TimePickerDialogIntervals timePickerDlg = new TimePickerDialogIntervals(this.Context, new EventHandler<TimePickerDialogIntervals.TimeSetEventArgs>(UpdateDuration),
            Element.Time.Hours, Element.Time.Minutes, true);

        var control = new EditText(this.Context);
        control.Focusable = false;
        control.FocusableInTouchMode = false;
        control.Clickable = false;
        control.Click += (sender, ea) => timePickerDlg.Show();
        control.Text = Element.Time.Hours.ToString("00") + ":" + Element.Time.Minutes.ToString("00");

        SetNativeControl(control);
    }

    void UpdateDuration(object sender, Android.App.TimePickerDialog.TimeSetEventArgs e)
    {
        Element.Time = new TimeSpan(e.HourOfDay, e.Minute, 0);
        Control.Text = Element.Time.Hours.ToString("00") + ":" + Element.Time.Minutes.ToString("00");
    }
}

public class TimePickerDialogIntervals : TimePickerDialog
{
    public const int TimePickerInterval = 05;
    private bool _ignoreEvent = false;

    public TimePickerDialogIntervals(Context context, EventHandler<TimePickerDialog.TimeSetEventArgs> callBack, int hourOfDay, int minute, bool is24HourView)
        : base(context, (sender, e) =>
        {
            callBack(sender, new TimePickerDialog.TimeSetEventArgs(e.HourOfDay, e.Minute * TimePickerInterval));
        }, hourOfDay, minute / TimePickerInterval, is24HourView)
    {
    }

    public override void OnTimeChanged(Android.Widget.TimePicker view, int hourOfDay, int minute)
    {
        base.OnTimeChanged(view, hourOfDay, minute);

        if (_ignoreEvent) return;

        if (minute % TimePickerInterval != 0)
        {
            int minuteFloor = minute - (minute % TimePickerInterval);
            minute = minuteFloor + (minute == minuteFloor + 1 ? TimePickerInterval : 0);
            if (minute == 60)
                minute = 0;
            _ignoreEvent = true;
            view.CurrentMinute = (Java.Lang.Integer)minute;
            _ignoreEvent = false;
        }
    }
}

为什么时钟弹出窗口关闭时 UI 上显示的时间不同?

问题是因为分钟已经乘以 'TimePickerInterval' (05)。将参数更改为 'e.Minute' 将按预期工作。

代码:

public class TimePickerDialogIntervals : TimePickerDialog
{
    public const int TimePickerInterval = 15;
    private bool _ignoreEvent = false;
    public TimePickerDialogIntervals(Context context, EventHandler<TimePickerDialog.TimeSetEventArgs> callBack, int hourOfDay, int minute, bool is24HourView) :
        base(context, (sender, e) =>
        {
            callBack(sender, new TimePickerDialog.TimeSetEventArgs(e.HourOfDay, e.Minute));//remove '* TimePickerInterval'
        }, hourOfDay, minute / TimePickerInterval, is24HourView)
    {
    }
    ...
}