Xamarin.Forms apps written between 2016 and 2022 often use MessagingCenter as the glue between view models, pages and platform code. It works. It is also string-keyed, global, and hard to test. In .NET MAUI it is marked obsolete in recent releases, so a migration is the natural time to replace it.
This post covers how to find every use, what to replace it with, and how to pin the behaviour before you change it.
Step 1: Inventory every subscription and send
Search the solution, including the iOS and Android projects. Platform code often sends messages that shared code receives.
grep -rn "MessagingCenter\.\(Subscribe\|Send\|Unsubscribe\)" --include=*.cs .
Build a table with four columns: message string, sender type, subscriber type, argument type. Two things usually show up:
- Subscribers with no sender. Dead code. Delete it.
- Senders with no subscriber. Often a feature removed years ago. Delete those too.
Messages that remain are your real contract.
Step 2: Choose the replacement
For most apps, WeakReferenceMessenger from the CommunityToolkit.Mvvm package is the closest match. It is weak-referenced, like MessagingCenter, and it uses typed messages instead of strings.
Not every message should become a messenger message. Sort each one:
| Pattern in the Xamarin code | Replace with |
|---|---|
| Page asks the view model to refresh after a child page saves | Return a result through navigation, or call a method on a shared service |
| Platform code tells shared code a push token arrived | A service interface registered in dependency injection |
| One view model tells another that the current user changed | WeakReferenceMessenger with a typed message |
| Message with a single sender and single subscriber, both alive together | A direct event or method call |
The honest trade-off: the messenger keeps the loose coupling you already have. A service interface removes it. Choose the messenger where you cannot cheaply introduce a shared owner, and choose a service where you can.
Step 3: Pin current behaviour first
Before editing, write characterization tests that record what happens today. MessagingCenter is static, so tests that touch it share state. Reset it between tests by unsubscribing in teardown.
A test for "saving a profile updates the header name" should assert on the subscriber's visible property after the sender runs. It should not assert on MessagingCenter itself. Then the same test passes before and after the swap. Our post on characterization tests around shared view models covers this approach in more detail.
Step 4: Convert one message
Before:
// Sender
MessagingCenter.Send<ProfileViewModel, string>(this, "ProfileNameChanged", Name);
// Subscriber
MessagingCenter.Subscribe<ProfileViewModel, string>(this, "ProfileNameChanged",
(sender, name) => HeaderName = name);
// Teardown
MessagingCenter.Unsubscribe<ProfileViewModel, string>(this, "ProfileNameChanged");
After:
// Message type
public sealed class ProfileNameChangedMessage : ValueChangedMessage<string>
{
public ProfileNameChangedMessage(string name) : base(name) { }
}
// Sender
WeakReferenceMessenger.Default.Send(new ProfileNameChangedMessage(Name));
// Subscriber
WeakReferenceMessenger.Default.Register<ShellViewModel, ProfileNameChangedMessage>(
this, static (recipient, message) => recipient.HeaderName = message.Value);
// Teardown
WeakReferenceMessenger.Default.UnregisterAll(this);
The compiler now checks the message type. A typo in a string key can no longer silently drop a message.
Step 5: Watch for these differences
- Threading. Neither API switches threads for you. If a subscriber updates UI and the sender runs on a background thread, marshal with
MainThread.BeginInvokeOnMainThread. Xamarin apps sometimes got away with this on Android and crashed on iOS. - Registration timing. A message sent before the subscriber registers is lost in both APIs. If a page registers in
OnAppearingand the message arrives earlier, that bug already exists. The migration will not fix it. - Duplicate registration. Registering the same recipient twice for the same message type throws in the messenger.
MessagingCentertolerated it and delivered twice. Duplicates you find are existing bugs. - Platform senders. Android
Activityor iOSAppDelegatecode that sends messages must now reference the message type from a shared project. That is a good moment to replace it with a service.
Step 6: Remove in slices
Convert one message family per pull request, run the characterization tests, and ship. Do not leave a message half-converted: one side sending through MessagingCenter and the other listening on the messenger fails silently.
When the last call is gone, add a build check that fails on new MessagingCenter references.
When to skip it
If a screen is on your retire list, do not convert its messages. Delete the screen and its subscriptions together. The screen triage checklist helps you decide.
Send us the repo
If you have a Xamarin app with message strings scattered across shared and platform projects, send us the repo. We will return a written plan: what to delete, what to convert, and in what order.