[英]DateTimePicker in Xamarin.Forms iOS
I'm creating an app with Xamarin Forms and I want to implement a datetimepicker like this in iOS我正在使用 Xamarin Forms 创建一个应用程序,我想在 iOS 中实现这样的日期时间选择器https://developer.apple.com/documentation/uikit/uidatepickerhttps://developer.apple.com/documentation/uikit/uidatepicker
I have found how to implement a TimePicker or a DatePicker but not all in one.我已经找到了如何实现 TimePicker 或 DatePicker 但不是全部。 Is there any way to add it to Xamarin Forms?有没有办法将它添加到 Xamarin Forms 中? Is there any way to create it in Xamarin Forms?有没有办法在 Xamarin Forms 中创建它?
<DatePicker
x:Name="inicioDatePicker"
Format="D"
DateSelected="OnDateSelected"
Date="{Binding InicioDatePicker}"/>
Thank you.谢谢你。 Regards问候
You should add Event when the value of Picker changed.Refer the following code 当Picker的值更改时,您应该添加Event。请参考以下代码
//...
using ObjCRuntime;
//...
protected override void OnElementChanged(ElementChangedEventArgs<DatePicker> e)
{
base.OnElementChanged(e);
if (Control != null)
{
UIDatePicker dateTimePicker = (UIDatePicker)Control.InputView;
dateTimePicker.Mode = UIDatePickerMode.DateAndTime;
dateTimePicker.AddTarget(this, new Selector("DateChanged:"), UIControlEvent.ValueChanged);
NSDateFormatter dateFormat = new NSDateFormatter();
dateFormat.DateFormat = "dd/MM/yyyy HH:mm";
var text = (UITextField)Control;
text.Text = dateFormat.ToString(dateTimePicker.Date);
}
}
[Export("DateChanged:")]
public void DateChanged(UIDatePicker picker)
{
NSDateFormatter dateFormat = new NSDateFormatter();
dateFormat.DateFormat = "dd/MM/yyyy HH:mm";
var text = (UITextField)Control;
text.Text = dateFormat.ToString(picker.Date);
}
I have managed to do it, but I can't format the date and time when I click on the done button.Does anyone know how to do it? 我已经设法做到了,但是当我点击完成按钮时我无法格式化日期和时间,有人知道怎么做吗?
This is my code: 这是我的代码:
using System;
using App.iOS;
using CoreGraphics;
using Foundation;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(DatePicker), typeof(CustomDatePickerRender))]
namespace App.iOS
{
public class CustomDatePickerRender : DatePickerRenderer
{
public CustomDatePickerRender()
{
}
protected override void OnElementChanged(ElementChangedEventArgs<DatePicker> e)
{
base.OnElementChanged(e);
if (Control != null)
{
var dateTimePicker = (UIDatePicker)Control.InputView;
dateTimePicker.Mode = UIDatePickerMode.DateAndTime;
NSDateFormatter dateFormat = new NSDateFormatter();
dateFormat.DateFormat = "dd/MM/yyyy HH:mm";
var text = (UITextField)Control;
text.Text = dateFormat.ToString(dateTimePicker.Date);
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.