Reputation: 97
I'm writing an Android renderer for a Xamarin Forms custom crontrol(CustomDatePicker). I found sample code which does the job. However there are a few lines of code that I do not understand. I'm referring to the first constructor parameter of DatePickerDialog which is a callback function. Could someone please explain the what this actually does and also if I really need all commands, for example
view.Date = e.Date ?
I'm already setting the date when "Done" button is clicked?? (this code exists).
[assembly: ExportRenderer(typeof(Common.Infrastructure.Controls.CustomDatePicker), typeof(CustomDatePickerRenderer))]
namespace Employer.Droid
{
public class CustomDatePickerRenderer : ViewRenderer<CustomDatePicker, EditText>
{
public CustomDatePickerRenderer(Context context) : base(context)
{
}
///more logic
void CreateDatePickerDialog(int year, int month, int day)
{
CustomDatePicker view = Element;
_dialog = new DatePickerDialog(Context, **(o, e) =>
{
view.Date = e.Date;
((IElementController)view).SetValueFromRenderer(VisualElement.IsFocusedProperty, false);
Control.ClearFocus();
_dialog = null;**
}, year, month, day);
_dialog.SetButton("Done", (sender, e) =>
{
SetDate(_dialog.DatePicker.DateTime);
});
_dialog.DatePicker.MinDate = (long)(DateTime.Now.Date - new DateTime(1970, 1, 1)).TotalMilliseconds;
}
Upvotes: 0
Views: 114
Reputation: 9356
The EventHandler<DateSetEventArgs> callBack
is the method that will be executed when a new date is set DatePicker.
In your case, you will use this method to send the value from your CustomRenderer
to your CustomDatePicker
in your Shared project.
I see you are also setting the value when the user clicks on the "Done" button so if you don't want to pass in an action value to the EventHandler<DateSetEventArgs> callBack
, you can send an empty one, something like:
_dialog = new DatePickerDialog(Context, (o, e) => { }, year, month, day);
Just make sure that on the SetDate
method you are doing all the required steps so the Date picked is correctly set into the Element
.
Hope this helps.-
Upvotes: 1