Search code examples
xamarinxamarin.formsdatepickerxamarin.ioscustom-renderer

Xamarin.Forms: close DatePicker in code, when custom renderer is used


I have a custom DatePicker, which is opened if the user taps on it. It uses the same mechanics like the Xamarin.Forms DatePicker. The scenario is like the following:

  • user taps on the custom DatePicker
  • instead of picking a date he opens another dialog
  • two dialogs are now opened at the same time

I tried to Unfocus() if the other element get's the focus, but nothing happens. The DatePicker is still displayed.

What else can I do? Should I throw manually the Unfocus event? Can I lock the UI somehow so that the user has to press the Finish button before moving on?


Solution

  • First, the issue doesn't occur if I switch between DatePicker and TimePicker. It only occurs if I switch from DatePicker/TimePicker to my Entry (which triggers a custom dialog in Focused event).

    And Unfocus() does work, if you do it on the UI thread:

    private async void someEntry_Focused(object sender, FocusEventArgs e)
    {
        Device.BeginInvokeOnMainThread(() =>
        {
            this.datePicker.Unfocus();
        });
        // custom dialog shown to user ...
    }