State Triggers

Learn how to use .NET MAUI state triggers.

State triggers are fired when the state of some element changes. For example, we might have a trigger that monitors the screen orientation. The setter on the trigger will then rearrange the elements depending on whether the screen is in portrait or landscape mode. There might also be a trigger that monitors the state of a specific property on a specific element. For example, we might have a trigger that monitors the dimensions of the app window and hides some elements when it becomes too small. There are multiple types of state triggers in .NET MAUI, but they all follow similar principles.

We'll cover state triggers in .NET MAUI with the aid of the following project setup:

<?xml version = "1.0" encoding = "UTF-8" ?>
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:StateTriggerDemo"
             x:Class="StateTriggerDemo.App">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources/Styles/Colors.xaml" />
                <ResourceDictionary Source="Resources/Styles/Styles.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>
.NET MAUI project example with state triggers demonstration

Adding a state trigger to a page

In the example above, we add global state triggers to the ContentPage.Resources element on lines 6–33 of the MainPage.xaml file. This is because we ...