I'm having trouble with adding a placeholder to my DatePicker (doesn't have title property like a regular picker). I have a custom renderer:
public class DOBPicker : DatePicker { public static readonly BindableProperty EnterTextProperty = BindableProperty.Create(propertyName: "Placeholder", returnType: typeof(string), declaringType: typeof(DOBPicker), defaultValue: default(string)); public string Placeholder { get; set; } }
iOS Renderer:
`protected override void OnElementChanged(ElementChangedEventArgs e)
{
base.OnElementChanged(e);
if (this.Control == null)
return;
var element = e.NewElement as DOBPicker;
if (!string.IsNullOrWhiteSpace(element.Placeholder))
{
Control.Text = element.Placeholder;
}
Control.ShouldEndEditing += (textField) => {
var seletedDate = (UITextField)textField;
var text = seletedDate.Text;
if (text == element.Placeholder)
{
Control.Text = DateTime.Now.ToString("dd/MMMM/yyyy");
}
return true;
};
}
private void OnCanceled(object sender, EventArgs e)
{
Control.ResignFirstResponder();
}
private void OnDone(object sender, EventArgs e)
{
Control.ResignFirstResponder();
}`
It works, but when I go back to the previous page, the app crashes and I get an error (Object reference not set to an instance of an object). I think it's referring to
if (!string.IsNullOrWhiteSpace(element.Placeholder)) { Control.Text = element.Placeholder; }