private Task <IDialogAnswer <DateTime> > ShowDateTimeDialog(string caption, UIDatePickerMode mode, DateTime current, IDialogButton positive, IDialogButton negative) { var tcs = new TaskCompletionSource <IDialogAnswer <DateTime> >(); var version = new Version(UIDevice.CurrentDevice.SystemVersion); if (version.Major >= 7) { var picker = new DatePicker(); picker.Title = caption; picker.NativeDatePicker.Mode = mode; picker.NativeDatePicker.Date = current; picker.DoneTitle = positive.Caption; picker.CancelTitle = negative.Caption; picker.Click += (sender, e) => { if (tcs.Task.Status < TaskStatus.RanToCompletion) { NSDate dateTime = (sender as DatePicker).NativeDatePicker.Date; DateTime result = dateTime != null ? System.DateTime.SpecifyKind(dateTime, DateTimeKind.Utc).ToLocalTime() : System.DateTime.MinValue; if (e.ButtonIndex == 0) { tcs.SetResult(new DialogAnswer <DateTime>(true, result)); } else if (e.ButtonIndex == 1) { tcs.SetResult(new DialogAnswer <DateTime>(false, result)); } } }; picker.Show(_context.MainController.View, (ScreenController)_context.MainController.VisibleViewController); } else { var alertView = new UIAlertView(caption, "", null, positive.Caption, negative.Caption); alertView.Show(); _alertViews.Add(alertView); var datePicker = new UIDatePicker(); datePicker.Frame = new RectangleF(10, alertView.Bounds.Size.Height, 270, 150); datePicker.Mode = UIDatePickerMode.Date; datePicker.Date = current; alertView.AddSubview(datePicker); var timePicker = new UIDatePicker(); timePicker.Frame = new RectangleF(10, alertView.Bounds.Size.Height + 150 + 20, 270, 150); timePicker.Mode = UIDatePickerMode.Time; timePicker.Date = current; alertView.AddSubview(timePicker); alertView.Clicked += (sender, e) => { DateTime date = System.DateTime.SpecifyKind(datePicker.Date, DateTimeKind.Unspecified); DateTime time = System.DateTime.SpecifyKind(timePicker.Date, DateTimeKind.Utc).ToLocalTime(); var result = new DateTime(date.Year, date.Month, date.Day, time.Hour, time.Minute, time.Second); if (e.ButtonIndex == 0) { tcs.SetResult(new DialogAnswer <DateTime>(true, result)); } else if (e.ButtonIndex == 1) { tcs.SetResult(new DialogAnswer <DateTime>(false, result)); } var av = (UIAlertView)sender; _alertViews.Remove(av); av.Dispose(); }; alertView.Bounds = new RectangleF(0, 0, 290, alertView.Bounds.Size.Height + 150 + 150 + 20 + 30); } return(tcs.Task); }
public void ShowDateTimeDialog(string caption, UIDatePickerMode mode, DateTime current, DialogButton <DateTime> positive, DialogButton <DateTime> negative) { Version version = new Version(UIDevice.CurrentDevice.SystemVersion); if (version.Major >= 7) { DatePicker picker = new DatePicker(); picker.Title = caption; picker.NativeDatePicker.Mode = mode; picker.NativeDatePicker.Date = current; picker.DoneTitle = positive.Caption; picker.CancelTitle = negative.Caption; picker.Click += (object sender, UIButtonEventArgs e) => { var dateTime = (sender as DatePicker).NativeDatePicker.Date; DateTime result = dateTime != null?System.DateTime.SpecifyKind(dateTime, DateTimeKind.Utc).ToLocalTime() : System.DateTime.MinValue; if (e.ButtonIndex == 0) { positive.Execute(result); } else if (e.ButtonIndex == 1) { negative.Execute(result); } }; picker.Show(_context.MainController.View, (ScreenController)_context.MainController.VisibleViewController); } else { var alertView = new UIAlertView(caption, "", null, positive.Caption, negative.Caption); alertView.Show(); _alertViews.Add(alertView); UIDatePicker datePicker = new UIDatePicker(); datePicker.Frame = new System.Drawing.RectangleF(10, alertView.Bounds.Size.Height, 270, 150); datePicker.Mode = UIDatePickerMode.Date; datePicker.Date = current; alertView.AddSubview(datePicker); UIDatePicker timePicker = new UIDatePicker(); timePicker.Frame = new System.Drawing.RectangleF(10, alertView.Bounds.Size.Height + 150 + 20, 270, 150); timePicker.Mode = UIDatePickerMode.Time; timePicker.Date = current; alertView.AddSubview(timePicker); alertView.Clicked += (object sender, UIButtonEventArgs e) => { DateTime date = System.DateTime.SpecifyKind(datePicker.Date, DateTimeKind.Unspecified); DateTime time = System.DateTime.SpecifyKind(timePicker.Date, DateTimeKind.Utc).ToLocalTime(); DateTime result = new DateTime(date.Year, date.Month, date.Day, time.Hour, time.Minute, time.Second); if (e.ButtonIndex == 0) { positive.Execute(result); } else if (e.ButtonIndex == 1) { negative.Execute(result); } var av = (UIAlertView)sender; _alertViews.Remove(av); av.Dispose(); }; alertView.Bounds = new System.Drawing.RectangleF(0, 0, 290, alertView.Bounds.Size.Height + 150 + 150 + 20 + 30); } }