为了详细说明 Jason 的答案,这里是一个使用 UIPickerView 作为自定义日期选择器的代码示例。希望它可以帮助某人。
UIPickerView datePicker = new UIPickerView ();
var mod = new DatePickerDataModel ();
datePicker.Model = mod;
mod.ValueChanged += (object sender, EventArgs e) => {
_selectedDate = mod.SelectedItem;
};
class DatePickerDataModel : UIPickerViewModel
{
public event EventHandler<EventArgs> ValueChanged;
public List<DateTime> Items { get; private set; }
public DateTime SelectedItem {
get { return Items [selectedIndex]; }
}
int selectedIndex = 0;
public DatePickerDataModel ()
{
var items = new List<DateTime> ();
for (int i = 0; i < 14; i++) {
items.Add (DateTime.Now.Subtract (TimeSpan.FromDays (i)));
}
Items = items;
}
public override nint GetRowsInComponent (UIPickerView picker, nint component)
{
return Items.Count;
}
public override nint GetComponentCount (UIPickerView picker)
{
return 1;
}
public override void Selected (UIPickerView picker, nint row, nint component)
{
selectedIndex = (int)row;
ValueChanged?.Invoke(this, EventArgs.Empty());
}
public override string GetTitle (UIPickerView pickerView, nint row, nint component)
{
var t = Items [(int)row];
var m = t.ToString ("MMM");
var d = t.ToString ("ddd");
return $"{d} {m} {t.Day}";
}
public override nfloat GetRowHeight (UIPickerView picker, nint component)
{
return 44;
}
}