pablogupi
pablogupi

Reputation: 774

DateTimePicker in Xamarin.Forms iOS

I'm creating an app with Xamarin Forms and I want to implement a datetimepicker like this in iOS https://developer.apple.com/documentation/uikit/uidatepicker

I have found how to implement a TimePicker or a DatePicker but not all in one. Is there any way to add it to Xamarin Forms? Is there any way to create it in Xamarin Forms?

 <DatePicker
   x:Name="inicioDatePicker"
   Format="D"
   DateSelected="OnDateSelected"
   Date="{Binding InicioDatePicker}"/>

Thank you. Regards

Upvotes: 0

Views: 1979

Answers (1)

Lucas Zhang
Lucas Zhang

Reputation: 18861

You should add Event when the value of Picker changed.Refer the following code

//...
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);
}

Upvotes: 3

Related Questions