Search⌘ K

Publishing and Subscribing to Messages

Understand how to implement message publishing and subscribing using the static MessagingCenter class in .NET MAUI. Learn to send, receive, and unsubscribe messages effectively for seamless communication between application components.

.NET MAUI has a built-in mechanism for publishing messages and subscribing to them. This mechanism allows us to share instructions between any components inside the application without them having to call each other's methods directly.

Publishing and subscribing are done by the static MessagingCenter class. Because this class is static, a subscriber to the message can be any object within the application. To demonstrate how it works, we have 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:MessageSubscriptionDemo"
             x:Class="MessageSubscriptionDemo.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 message subscription demonstration

Publishing messages

...