I recently implemented dark mode in iOS for an app using Xamarin for app-development. It turned out to be quite a bit of work, as all styles had to be changed / rewritten, and I had to go through all the code in the entire app and change styling - either background color or text color or what not.
In Xamarin, you also have to use the "Experimental flag" when implementing dark mode. To set this flag, I followed the instructions in this article by Microsoft - https://docs.microsoft.com/en-us/xamarin/xamarin-forms/internals/experimental-flags.
As you can see in this article, you are told to set the "AppTheme_Experimental" flag in your code. There are several ways to do this, and I unfortunately used this code in my AppDelegate.cs class in my DoStartupWork() method to enable the flag with a line of code like this: "Xamarin.Forms.Forms.SetFlags("AppTheme_Experimental");". This worked all fine while in debug mode - AND using the "Linker behaviour: Don't Link". Before publishing my app, I wanted to test the new dark mode functionality on a real device. I then switched to a new setting with "Linker behaviour: Link Framework SDKs Only", and my app started crashing right after the launch screen was displayed...
It actually took me a while to figure out this error. It turned out that the explanation is in the above mentioned article. The key here are the words "platform project". They state the following:
What I didn't realize was that they had another section where they talked about how to enable an experimental flag for a shared code project. And the key here is "shared" as opposed to "platform". So for shared code projects, they state the following:
Based on this information, I then added the code "Device.SetFlags(new string[]{ "AppTheme_Experimental" });" in my App.xaml.cs - remember to put it before the InitializeComponent() method (it should occur before the Forms.Init method is invoked). And I of course had to remove the previous line of code in the AppDelegate.cs class.
Voila! It worked again. No more crashes.
Well, you can use Xamarin to develop shared code projects (i.e., code shared between iOS, Android and UWP), or you can use Xamarin to develop platform projects (i.e., separate code for iOS, Android, UWP). The thing is that in shared code projects, the file AppDelegate.cs is also present...
André Vold
Her er en rask liten BLOGGER BIO for å fortelle litt om min bakgrunn - og hvorfor jeg valgte programmering som fagfelt.