In a previous post, we looked at how you could set platform specific values with the
Device.OnPlatform<T> static method. Today we will see how we can achieve the same result by using the
<OnPlatform> markup in a
The problem we were facing was that some extra padding was required at the top for a single-page application, but only on iOS. We ended up with the following code in the code behind file:
We can express a similar code in
XAML markup only! The
XAML version would look something like that:
So what’s going on here? The
Device.OnPlatform<T> method is in a generic method. While the generic type was implicitly defined in the code version based on the context, this is not possible in
XAML and it needs to be defined explicitly.
x:TypeArguments attributes is available for that purpose. We assigned it the type
Thickness which is the type of the
Padding property. Then we can define for each platform the instance of
Thickness that will be created.
Now there is a little trick that we can use to simplify the code even more. Since the default values for padding are the one for
WinPhone, we can remove these entries from the
Lastly, because the property
Thickness can be represented by a single string, we can even define the property as an attribute like so
The final version is clear and precise.
What do you use? Do you prefer the
C# or the
XAML way of specifying UI properties for specific platforms?