A known issue in Xamarin.iOS and Xamarin.Android is that, when we enable linker (Link All), some code fails in run time. I faced this issue in my project, everything worked well using the NuGet package when Linker setting is set to "Don't Link". But after setting linker to "Link All" it raises error:
Steps to repeat the problem:
1- Create Xamarin.iOS project.
2- Call TZNames.GetDisplayNames().
3- Exception is thrown right in this line in point 2.
Newtonsoft.Json.JsonSerializationException: 'Unable to find a constructor to use for type TimeZoneNames.TimeZoneData. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'DisplayNames', line 1, position 16.'
Solution that I have done is to download the source and modify source. It was a simple solution but little bit tricky.
Steps for solution:
1- Remove NuGet Package (TimeZoneNames).
2- Download the git repository then add the project (TimeZoneNames) directly in your solution.
3- Add the following code snippet at the end of (TZNames.cs):
static public void Preserver() { new TimeZoneData(); new CldrLanguageData(); new TimeZoneSelectionData(); }
4- In the iOS project, create a static class and add this static method in it:
[Preserve(AllMembers = true)] static class Preserver { static void EnsurePreserved() { TimeZoneNames.TZNames.Preserver(); } }
5- You are done !
A known issue in Xamarin.iOS and Xamarin.Android is that, when we enable linker (Link All), some code fails in run time. I faced this issue in my project, everything worked well using the NuGet package when Linker setting is set to "Don't Link". But after setting linker to "Link All" it raises error:
Steps to repeat the problem: 1- Create Xamarin.iOS project. 2- Call TZNames.GetDisplayNames(). 3- Exception is thrown right in this line in point 2.
Newtonsoft.Json.JsonSerializationException: 'Unable to find a constructor to use for type TimeZoneNames.TimeZoneData. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'DisplayNames', line 1, position 16.'
Solution that I have done is to download the source and modify source. It was a simple solution but little bit tricky.Steps for solution: 1- Remove NuGet Package (TimeZoneNames). 2- Download the git repository then add the project (TimeZoneNames) directly in your solution. 3- Add the following code snippet at the end of (TZNames.cs):
static public void Preserver() { new TimeZoneData(); new CldrLanguageData(); new TimeZoneSelectionData(); }
4- In the iOS project, create a static class and add this static method in it:[Preserve(AllMembers = true)] static class Preserver { static void EnsurePreserved() { TimeZoneNames.TZNames.Preserver(); } }
5- You are done !