Important OnPlatform Changes in Xamarin.Forms
Last week I blogged about the important changes to Device.OS in the latest version of Xamarin.Forms. Well, I am back with some other changes from that same exact Pull Request that have to do with OnPlatform in code and XAML that use the new Device.RuntimePlatform.
So, what is OnPlatform? It allows you to set properties on elements or run specific code based on specific platforms. This is really nifty for tiny tweaks to UI and can be done completely in the XAML, which is awesome. To make way for furture enhancements there have been small tweaks to the XAML and code behind implementations.
First, XAML. Instead of having to support all the crazy inline options for hard coded platforms we can simply use string versions of each platform. Here is a before and after:
Easy peasy to update actually and allows for custom platforms in the future.
How about the favorite Device.OnPlatform method? Well I never really liked this method to be honest with you and apparently it wasn’t very flexible long term. So instead of passing in a bunch of Actions into a crazy method the new recommendation is to simply do a switch case over Device.RuntimePlatform! Check it out:
There you have it, quick and simple changes available now in 2.3.4 of Xamarin.Forms.