We override TimePickerRenderer on Android to enable 24-hour time picking. This works on most devices but crashes on Thai.
The error is a common one experienced by many developers (with many threads about fixing similar issues, e.g. https://forums.xamarin.com/discussion/42899/datetime-tostring-throws-argumentoutofrangeexception-in-thai-locale)
The error seems to be in:
https://github.com/xamarin/Xamarin.Forms/blob/5787584c86f2ca44915ecaf0e690789cb7922834/Xamarin.Forms.Platform.Android/Renderers/TimePickerRenderer.cs#L138 The ToString() should include a "CultureInfo.InvariantCulture" argument.
Don't crash.
Crash
https://us.v-cdn.net/5019960/uploads/editor/m6/5ozwodcv067k.png
Additionally it is non-trivial to copy the picker source code and embed it in a project (many references to XF internal stuff), it may be convenient to expose some of those to enable local fixes.
We worked around this issue by calling from the App constructor:
cultureInfo = LocalizationHelper.AppCulture;
System.Threading.Thread.CurrentThread.CurrentCulture = cultureInfo;
CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
this works for us as long as we don't officially support Thai/Arabic, but a more permanent fix would be ideal.
@pfaucon Which version of Xamarin.Forms are you using? And what does your custom renderer look like (i.e., where/how are you changing the control to use the 24-hour format)?
Side note: the XF TimePicker will use a 24-hour format automatically if the device time format settings are 24-hour. I realize this may not apply to the issue you are addressing with your custom renderer, but I thought I'd mention it just in case.
That's very odd, I tried to make a reproduction but it seems to work just fine there, but in our app (without the fix) the stack dump is: here
The source for the renderer is here, the same as https://forums.xamarin.com/discussion/19657/timepicker-with-24-hour-format-dialog
Edit: and XF is 3.1.0.697729 (latest stable)
See also the update in this comment downthread
I was able to reproduce the issue using your custom renderer; to do so, I had to disable the "Use Shared Runtime" option in my Android project.
The "Not a valid calendar for the given culture" issue comes up because the assembly with the correct calendar type is not included by default in a Xamarin.Android application. In VS for Windows, open the Android project property pages, go to Android Options, And under "Additional Supported Encodings" make sure to include "Other"; this will include the encodings for Cyrillic, Baltic, Vietnamese, Ukrainian, and Thai.
If you have Linking set to "None", you should just be able to rebuild and the TimePicker will stop crashing. If you have Linking set to anything else, you'll also need to add some exceptions to "Skip linking assemblies"; otherwise, the linker will remove the calendar information you need.
These are the "Skip linking assemblies" options which worked for me:
I18N;I18N.Other;System;mscorlib;netstandard;System.Globalization
Let me know if that addresses your issues.
@pfaucon Since we haven't heard from you in more than 30 days, we hope that this issue is no longer affecting you. If it is, please feel free to reopen this issue and provide the requested information. Thank you for your report!
After investigating this further, it appears that if you only need to address the "Not a valid calendar for the given culture" issue for the Thai calendar, you actually do _not_ need to include the "Other" encoding and for "Skip linking assemblies" it is sufficient to simply set the value to netstandard.
Most helpful comment
See also the update in this comment downthread
I was able to reproduce the issue using your custom renderer; to do so, I had to disable the "Use Shared Runtime" option in my Android project.
The "Not a valid calendar for the given culture" issue comes up because the assembly with the correct calendar type is not included by default in a Xamarin.Android application. In VS for Windows, open the Android project property pages, go to Android Options, And under "Additional Supported Encodings" make sure to include "Other"; this will include the encodings for Cyrillic, Baltic, Vietnamese, Ukrainian, and Thai.
If you have Linking set to "None", you should just be able to rebuild and the TimePicker will stop crashing. If you have Linking set to anything else, you'll also need to add some exceptions to "Skip linking assemblies"; otherwise, the linker will remove the calendar information you need.
These are the "Skip linking assemblies" options which worked for me:
I18N;I18N.Other;System;mscorlib;netstandard;System.GlobalizationLet me know if that addresses your issues.