Code snippets for Xamarin Forms XAML Platform-specifics
This post is part of Luis Matos Code Snippets’ Xamarin Month
In the early days of Xamarin Forms, if you wanted to use a platform-specific feature of a page or a view, you’d have to write a custom renderer.
Later came effects, which were a little less painful.
But the game-changer was when Microsoft decided to build-in common platform-specific features, right into the XAML, so that you could turn on a platforms-specific feature for one platform only, with the directive being ignored for others.
The canonical example in my mind is telling iOS to use the Safe Area, depending on the specific phone, so parts of your UI are not cut off on certain phones. But here is the thing: I can never remember the magical incantation you use to do that. I have to google Xamarin ios safe area every time. There has to be a better way.
xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"
ios:Page.UseSafeArea="true"
Of course there is - a Code Snippet. Now all I have to remember is psios
to include the namespace and then pssa
to include the safe-area platform specific … I type the snippet, and hit tab, and bingo, the iOS Platform-specific XAML is entered into the editor (ios:Page.UseSafeArea="true"
in case you were wondering).
No more googling. In that spirit I’ve defined a whole slew of snippets for these platform-specifics, including one to import the ios
namespace used in the pssa
snippet.
The snippet file is here. To use it go to Tools->Code Snippets Manager and hit Import and import this file. Here are some of the Snippets I’ve defined
Shortcut | Explanation |
---|---|
psios | Snippet to bring in the iOS namespace for platform specific code |
pssa | Snippet for a iOS safe area |
psnpbs | Hide NavigationPage Bar Separator on iOS |
psmdps | Apply MasterDetailPage Shadow on iOS |
pslpt | Show Large Page Titles on iOS |
pshpi | Show Home Indicator Visibility on iOS |
pspsv | Set Page Status Bar Visibility on iOS |
There are a lot more platform-specifics than these, and of course they are available for Android too. If I find myself typing them more than once, I’ll create a snippet.
But really, if you find yourself typing the same thing over and over, do yourself a favor and define some snippets. It’s why you became a programmer in the first place, remember? Make the computer do the easy stuff for you, and you can focus on the hard stuff.