Moje rozwiązanie dla Xamarin.Android z MvvmCross na podstawie Linha:
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();
}
}
W przypadku waniliowej wersji Xamarin.Android wystarczy zastąpić WeakSubscribe zwykłą subskrypcją zdarzenia Click w EditText, nie zapominając o anulowaniu subskrypcji w przypadku zastąpienia metody Dispose.