Мое решение для Xamarin.Android с MvvmCross на основе Линь:
public class DatePickerEditText : EditText, DatePickerDialog.IOnDateSetListener
{
IDisposable _clickSubscription;
public override bool Clickable => true;
protected DatePickerEditText(IntPtr javaReference, JniHandleOwnership transfer)
: base(javaReference, transfer)
=> Init();
public DatePickerEditText(Context context) : base(context)
=> Init();
public DatePickerEditText(Context context, IAttributeSet attrs)
: base(context, attrs)
=> Init();
public DatePickerEditText(Context context, IAttributeSet attrs, int defStyleAttr)
: base(context, attrs, defStyleAttr)
=> Init();
public DatePickerEditText(Context context, IAttributeSet attrs, int defStyleAttr, int defStyleRes)
: base(context, attrs, defStyleAttr, defStyleRes)
=> Init();
protected override void Dispose(bool disposing)
{
if (disposing)
{
_clickSubscription?.Dispose();
_clickSubscription = null;
}
base.Dispose(disposing);
}
public void OnDateSet(DatePicker view, int year, int month, int dayOfMonth)
=> Text = view.DateTime.ToString("d", CultureInfo.CurrentUICulture);
void Init()
{
SetFocusable(ViewFocusability.NotFocusable);
_clickSubscription = this.WeakSubscribe(nameof(Click), OnClick);
}
void OnClick(object sender, EventArgs e)
{
var date = DateTime.Today;
try
{
date = DateTime.Parse(Text, CultureInfo.CurrentUICulture);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex);
}
var dialog = new DatePickerDialog(Context,
this,
date.Year,
date.Month,
date.Day);
dialog.Show();
}
}
Для ванильной версии Xamarin.Android просто замените WeakSubscribe обычной подпиской на событие Click EditText, не забывая отписаться в переопределении метода Dispose.