Plugin.Maui.MessagingCenter
provides a drop-in compatible replacement for the .NET MAUI MessagingCenter which has been deprecated and will be removed in the near future. This is a wrapper library that uses the method signatures of the .NET MAUI MessagingCenter but under the hood uses the MVVM Toolkit WeakReferenceMessenger which is known for its superb performance!
Please note that you probably want to adopt the MVVM Toolkit Messenger APIs as they provide more functionality.
Important
This library is meant to make the migration from Xamarin.Forms to .NET MAUI easier, not to provide a long-term solution.
Available on NuGet.
Install with the dotnet CLI: dotnet add package Plugin.Maui.MessagingCenter
, or through the NuGet Package Manager in Visual Studio.
After installation, add the using statement to your files:
using Plugin.Maui.MessagingCenter;
Or add it globally in your GlobalUsings.cs
file:
global using Plugin.Maui.MessagingCenter;
That's it! You can now use the MessagingCenter
class just like you did in .NET MAUI. Since the API is compatible with the .NET MAUI MessagingCenter, you can use it in the same way as before, there should be no need to change your existing code.
// Send a message with data
MessagingCenter.Send<MainPage, string>(this, "LocationUpdate", "New York");
// In another class, subscribe to receive the message
MessagingCenter.Subscribe<MainPage, string>(this, "LocationUpdate", (sender, location) =>
{
// Handle the location update
DisplayAlert("Location", $"New location: {location}", "OK");
});
// Send a simple notification message
MessagingCenter.Send<MainPage>(this, "RefreshData");
// Subscribe to the notification
MessagingCenter.Subscribe<MainPage>(this, "RefreshData", (sender) =>
{
// Refresh your data
LoadData();
});
public class MainViewModel : INotifyPropertyChanged
{
public void NotifyDataChanged()
{
// Send message from ViewModel
MessagingCenter.Send<MainViewModel, DataModel>(this, "DataUpdated", newData);
}
}
public partial class DetailPage : ContentPage
{
public DetailPage()
{
InitializeComponent();
// Subscribe to ViewModel messages
MessagingCenter.Subscribe<MainViewModel, DataModel>(this, "DataUpdated", (sender, data) =>
{
// Update UI with new data
UpdateDisplay(data);
});
}
}
Always unsubscribe when your object is disposed to prevent memory leaks:
public partial class MyPage : ContentPage
{
protected override void OnDisappearing()
{
// Unsubscribe from all messages this page subscribed to
MessagingCenter.Unsubscribe<MainViewModel, DataModel>(this, "DataUpdated");
MessagingCenter.Unsubscribe<MainPage>(this, "RefreshData");
base.OnDisappearing();
}
}
You can filter messages to only receive them from specific senders:
// Only receive messages from a specific instance
var specificViewModel = new MainViewModel();
MessagingCenter.Subscribe<MainViewModel, string>(this, "StatusUpdate",
(sender, status) => {
// Handle status update
}, specificViewModel); // Only from this specific instance
The API is compatible with the .NET MAUI MessagingCenter APIs. For more detailed documentation, see the .NET MAUI MessagingCenter reference and the MVVM Toolkit Messenger documentation.
- Multiple subscriptions to the same message type by the same subscriber will throw an
InvalidOperationException
- This prevents accidental duplicate subscriptions and potential memory leaks
- If you need multiple handlers, consider using different message names or consolidating logic into a single handler
For detailed information about behavioral differences, see BEHAVIOR_DIFFERENCES.md.
The bubble icon has kindly been provided by Smashicons