ContentPage or ContentPage XAML ,Which is better to use for page design while developing Xamarin Forms application? Is there any advantage for one over another in app performance?
1 Answers
XAML vs code is entirely opinion based. The general opinion seems to be that XAML as a markup language is much better suited and provides a much cleaner separation of the UI from the app logic. I personally would never think of doing pages in code, XAML all the way. Another point in favor of XAML is that if you use events or reference other controls (which, let's face it, every app does) you cannot even nest a bunch of initializers to build out a structured UI, you have to assign controls to named variables to be able to reference them. In XAML, you can add an x:Name at any point and you have a reference to that control from both code and the rest of the XAML markup.
The only thing you can't do in XAML is to dynamically programatically generate the layout or adjust it based on some runtime variables. But even here, it's best to layout the template in XAML, and then in code behind you can use the constructor (or override onappearing, if you want the page to change each time it's navigated to), and fill out the dynamic part from c# code.
Performance wise, there used to be a slight advantage to code pages, as XAML had to be parsed each time during runtime. However, for a while now, Xamarin.Forms has supported compiling the XAML files ahead of time, which means that they are more or less identical.