Xamarin Forms – Android – Exception in DatePicker

How to reproduce it

Recently I came across with an interesting bug in Xamarin Forms.

The bug is in the DatePicker control. A DatePicker is, as the name states, a control to pick a specific date in the calendar. It’s displayed like this:

It renders in Android, first, like an Xamarin Forms Entry, and when you tap on it, it displayed the pop up with days/months/years.

The thing is that if you quickly double tap, the code doesn’t prevent the “double tap protection”. So, double tap gives you two popups, and when you close the first-opened popup, you get an unhandled exception and you app closed itself.

The bug is already reported here:

And the culprit:


One solution

So a possible quick solution goes like this:

<StackLayout Grid.Column="1" Grid.Row="0"  HorizontalOptions="FillAndExpand" Padding="5, 0" VerticalOptions="Center">
                <TapGestureRecognizer NumberOfTapsRequired="1" Tapped="DateFromTapped"></TapGestureRecognizer>
        <Label Text="{Binding FormattedDateFrom}" VerticalOptions="End" HorizontalOptions="FillAndExpand" TextColor="Black">
                        <TapGestureRecognizer NumberOfTapsRequired="1" Tapped="DateFromTapped"></TapGestureRecognizer>
        <DatePicker Grid.Column="1" Grid.Row="0" Date="{Binding From}" x:Name="DateFrom" IsVisible="False"></DatePicker>
        <BoxView BackgroundColor="Black" HeightRequest="1" HorizontalOptions="FillAndExpand" VerticalOptions="End">
                        <TapGestureRecognizer NumberOfTapsRequired="1" Tapped="DateFromTapped"></TapGestureRecognizer>

What basically do is to create a hidden DatePicker, and handle the open/close of that DatePicker through a StackLayout and a Label.

That is really what a DatePicker is behind the scene: an Entry plus Labels plus a popup.

Hope that helps.

2018-09-20T19:43:57-03:00By |