Xamarin.ios 在 AlertController 中添加 DatePickerView 和 UIPickerView

Xamarin.ios add DatePickerView and PickerView in AlertController

可以像这样在 xamarin.ios 的 Alert 控制器中放置 DatePicker 和 PickerView 吗? Example image

我试过这个:

UIDatePicker datePickerView = new UIDatePicker(new CGRect(0, 0, this.View.Frame.Width, 300));
        var calendar = new NSCalendar(NSCalendarType.Gregorian);
        var currentDate = NSDate.Now;
        var components = new NSDateComponents();

        components.Year = -60;

        NSDate minDate = calendar.DateByAddingComponents(components, NSDate.Now, NSCalendarOptions.None);

        datePickerView.MinimumDate = minDate;
        datePickerView.MaximumDate = NSDate.Now;
        datePickerView.Mode = UIDatePickerMode.Date;


        UIAlertController actionSheetAlert = UIAlertController.Create("Data di nascita", "Seleziona un'opzione dalla lista", UIAlertControllerStyle.ActionSheet);


        actionSheetAlert.View.AddSubview(datePickerView);

        actionSheetAlert.AddAction(UIAlertAction.Create("Aggiungi", UIAlertActionStyle.Default, (action) => {

        }));

        actionSheetAlert.AddAction(UIAlertAction.Create("Cancel", UIAlertActionStyle.Destructive, (action) => Console.WriteLine("Cancel button pressed.")));


        this.PresentViewController(actionSheetAlert, true, null);

这是结果: Result image

你能提供一个完整的例子吗?谢谢!!!!

来自 Apple Documentation :

The UIAlertController class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.

您可以使用自定义控制器或视图,您将从头开始构建,您可以模态呈现和关闭并完全控制。

我认为您可以在 AlertController 上获得的最高级别的定制是您可以在这里找到的:

Tutorial to add textfields / buttons on alert

我想我们可以创建我们自己的 UITextField 来实现您截图的效果,这里是我的自定义文本字段供您参考:

public class MyTextField : UITextField
{
    private UIDatePicker inputView;
    private UIView inputAccessoryView;
    public override UIView InputView
    {
        get
        {
            if (inputView == null)
            {
                UIDatePicker datePickerView = new UIDatePicker();
                var calendar = new NSCalendar(NSCalendarType.Gregorian);
                var currentDate = NSDate.Now;
                var components = new NSDateComponents();

                components.Year = -60;

                NSDate minDate = calendar.DateByAddingComponents(components, NSDate.Now, NSCalendarOptions.None);

                datePickerView.MinimumDate = minDate;
                datePickerView.MaximumDate = NSDate.Now;
                datePickerView.Mode = UIDatePickerMode.Date;

                inputView = datePickerView;
            }
            return inputView;
        }
    }

    public override UIView InputAccessoryView
    {
        get
        {
            if (inputAccessoryView == null)
            {
                UIToolbar toolbar = new UIToolbar(new CGRect(0, 0, UIScreen.MainScreen.Bounds.Size.Width, 44));
                UIBarButtonItem item = new UIBarButtonItem(UIBarButtonSystemItem.Done, (sender, args) =>
                {
                    NSDateFormatter formatter = new NSDateFormatter();
                    formatter.DateFormat = "dd/MM/yyyy HH:mm";
                    this.Text = formatter.ToString(inputView.Date);
                    this.ResignFirstResponder();
                });
                UIBarButtonItem flexibleItem = new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace, null);
                toolbar.SetItems((new UIBarButtonItem[] { flexibleItem, item }), false);

                inputAccessoryView = toolbar;
            }
            return inputAccessoryView;
        }
    }
}

覆盖 InputView(将您的日期选择器放入其中)和 InputAccessoryView(带有 "Done" 的工具栏)

终于可以像这样使用了:

MyTextField textField = new MyTextField();
textField.Placeholder = "Enter something";
textField.Frame = new CGRect(0, 100, 200, 200);
View.AddSubview(textField);