XAML Views

Learn how to work with code-behind files in a .NET MAUI application.

XAML files in MAUI rarely work on their own. Typically, each of these files has a code-behind file attached to it. The code-behind file contains a standard C# class. But the XAML file can interoperate with it. The XAML file is interpreted by the compiler as a part of the same class. So, the XAML file and the code-behind associated with it are typically interpreted as two parts of the same partial class.

To demonstrate how code-behind interoperates with XAML, we have the following MAUI project inside an interactive code widget.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#512BD4</color>
    <color name="colorPrimaryDark">#2B0B98</color>
    <color name="colorAccent">#2B0B98</color>
</resources>
Baseline MAUI project setup

Referencing code-behind from XAML

In the above example, we have the MainPage.xaml file that represents the main page of the application. It has the ...