Start course

While XAML is the preferred method for designing a Xamarin Forms UI, whatever can be achieved in XAML can be done using C# in the code-behind file. Building a Xamarin Forms UI - Code Behind looks at manipulating controls in Xamarin Forms UI using C# code. We take a content view from the earlier Layout Containers course and see how to adapt to changing device orientation in the code-behind file.

Source code


Whatever you can do in XAML, you can do programmatically in the code-behind file.  As Xamarin Forms is based on an object-oriented hierarchy of UI elements, you can create new instances of a user interface control, set its properties, and call its methods with code. Controls or elements created in XAML can be accessed in the code-behind file by name, or by index if they belong to the child collection of another control. While you can code an app's entire UI in the code-behind file, XAML was developed to separate the user interface from functional code. From a development point of view, the combination of XAML and hot-reload is a massive time saver for fine-tuning the interface. Having to recompile and run the app every time you change a UI element in the code-behind file is literally programming from the last century. As we've seen, manipulating the UI with code does have its place but should be the exception rather than the rule. My name is Hallam Webber, and I hope you've found this Building a Xamarin Forms UI: Code Behind course instructive.

About the Author
Learning Paths

Hallam is a software architect with over 20 years experience across a wide range of industries. He began his software career as a  Delphi/Interbase disciple but changed his allegiance to Microsoft with its deep and broad ecosystem. While Hallam has designed and crafted custom software utilizing web, mobile and desktop technologies, good quality reliable data is the key to a successful solution. The challenge of quickly turning data into useful information for digestion by humans and machines has led Hallam to specialize in database design and process automation. Showing customers how leverage new technology to change and improve their business processes is one of the key drivers keeping Hallam coming back to the keyboard. 

Covered Topics