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

编程入门 行业动态 更新时间:2024-10-26 00:25:43
本文介绍了Xamarin Forms:在 TimePicker 中关闭时钟弹出窗口后,UI 上显示不同的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我使用下面的线程来设置时间选择器中 5 的倍数.使用自定义渲染器,我可以选择 5 的倍数.但是在选择时间后,当时钟弹出窗口关闭时,UI 上会显示不同的时间.问题仅在 android 平台上,对于 ios,一切正常.

I am using the below thread for setting the time in multiples of 5 in the time picker. Using the custom renderers I am able to select the time in multiples of 5. But after selecting a time, when the clock-Pop up closed, a different time is showing on the UI. The issue is only in the android platform, for the ios everything is working as excepted.

我的代码:

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 上显示不同的时间?

Why a different time is showing on the UI when the clock pop-up closed?

推荐答案

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

The problem is because the minute has been multiplied by 'TimePickerInterval' (05). Changing the parameter to 'e.Minute' will work as expected.

代码:

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)
    {
    }
    ...
}

这篇关于Xamarin Forms:在 TimePicker 中关闭时钟弹出窗口后,UI 上显示不同的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 11:37:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1393621.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时钟   弹出窗口   时间   Forms   Xamarin

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!