A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
Hi @Trung Huynh ,
Thanks for reaching out.
There isn’t only one correct approach, there are multiple ways. I recommend this order, from simple to complex:
- For small tweaks like font size, padding, color or margin,... We use
OnPlatformdirectly in XAML. For example:
<Label FontSize="{OnPlatform Android=18, iOS=20, Default=16}"
Padding="{OnPlatform Android=10, iOS=15}" />
This makes zero code pollution, stays declaritive.
- For repeated styles or app-wide: create platform-specific
ResourceDictionaryfiles inPlatforms/Android/Resources/Styles.xaml, then merge them conditionally inApp.xaml.csor viaMergedDictionarieswith #if directives. → Keeps shared XAML clean. - For complex logic, behaviors, custom rendering, native APIs,... We defined an interfacte in shared code. For ex: implement it in
Platforms/Android/andPlatforms/iOS/folders → Register via dependency injection inMauiProgram.cs.
Hope this helps. If you found my response helpful or informative, I would greatly appreciate it if you could follow this guidance provide feedback. Thank you.