c# - 自定义呈现器 DatePicker CountDownTimer 不会在 Xamarin 表单中更改值

标签 c# xamarin xamarin.forms xamarin.ios

因此,我正在尝试创建一个时间选择器,用户可以在其中选择一个计数仍然从哪里开始的数字。所以我发现你可以为此做一个日期选择器渲染器并使用一个叫做 CountDownTimer 的东西,很公平,我实现了这个并且当我尝试使用时它看起来不错!

但是现在,当我尝试在进行更改后获取值时,它不会触发任何事件,如果触发,它不会返回预期值,而是返回实际日期。如您所见,我尝试了多种方法。

public class DatePickerRenderer_iOS : DatePickerRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<DatePicker> e)
    {
        base.OnElementChanged(e);

        if (e.NewElement != null)
        {
            var picker = Control.InputView as UIDatePicker;
            if (picker != null)
            {
                picker.Mode = UIDatePickerMode.CountDownTimer;
            }
        }
    }
}

我如何使用它(pickerTime 是我在 XAML 中的选择器名称):

    void PickerTime_Unfocused(object sender, FocusEventArgs e)
    {
        System.Diagnostics.Debug.WriteLine(pickerTime.Date); // Triggers but returns a datetime, not the actualy selected hour + minute.
    }


    void PickerTime_PropertyChanging(object sender, PropertyChangingEventArgs e)
    {
        System.Diagnostics.Debug.WriteLine(pickerTime.Date);
    }


    private void DatePicker_DateSelected(object sender, DateChangedEventArgs e)
    {
        System.Diagnostics.Debug.WriteLine(e.NewDate);
    }

关于如何调整它以便我在日期选择器更改时直接获得更新值的任何想法?我将如何获得正确的值(不是日期,而是分钟 + 小时值,就像 CountDownTimer 实现一样)?

最佳答案

基础 DatePickerDate 属性自然是 DateTime 类型。因此,为了从 DateTime 中获取准确的分钟和小时;

var date = pickerTime.Date;
string hourMinute = date.ToString("HH:mm"); 

关于c# - 自定义呈现器 DatePicker CountDownTimer 不会在 Xamarin 表单中更改值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53601851/

相关文章:

c# - 解析 "querystring"格式数据的最简单方法

c# - 使用 asp 服务器控件 DataList 时,如何将 Eval 数据绑定(bind)表达式绑定(bind)到 Label?

c# - Linq to Sql - SelectMany

c# - 找不到 Xamarin.Forms.Platform.Android.LabelRenderer(xamarin 表单)的构造函数

c# - 无法从异步函数启动新的 Intent Activity

ios - Xamarin IOS 应用程序仅在安装 itunesconnect 和物理设备后崩溃

c# - Xamarin 表单按钮在 iOS 上带有文本和矢量图像

visual-studio - 使用 Xamarin 表单在 Visual Studio 中的 XAML 和代码之间切换

c# - 如何通过一次调用 Save 在 NHibernate 中更新一行并自动插入一个新行?

xamarin - 如何制作圆角入口控件 Xamarin.Forms