Plugin.Maui.MessagingCenter 1.0.0

dotnet add package Plugin.Maui.MessagingCenter --version 1.0.0
                    
NuGet\Install-Package Plugin.Maui.MessagingCenter -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Plugin.Maui.MessagingCenter" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Plugin.Maui.MessagingCenter" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Plugin.Maui.MessagingCenter" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Plugin.Maui.MessagingCenter --version 1.0.0
                    
#r "nuget: Plugin.Maui.MessagingCenter, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=Plugin.Maui.MessagingCenter&version=1.0.0
                    
Install Plugin.Maui.MessagingCenter as a Cake Addin
#tool nuget:?package=Plugin.Maui.MessagingCenter&version=1.0.0
                    
Install Plugin.Maui.MessagingCenter as a Cake Tool

alternate text is missing from this package README image

Plugin.Maui.MessagingCenter

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.

This library is meant to make the migration from Xamarin.Forms to .NET MAUI easier, not to provide a long-term solution.

Install Plugin

NuGet

Available on NuGet.

Install with the dotnet CLI: dotnet add package Plugin.Maui.MessagingCenter, or through the NuGet Package Manager in Visual Studio.

Getting Started

1. Add the Using Statement

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.

2. Basic Usage Examples

Sending Messages with Arguments
// 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");
});
Sending Messages without Arguments
// Send a simple notification message
MessagingCenter.Send<MainPage>(this, "RefreshData");

// Subscribe to the notification
MessagingCenter.Subscribe<MainPage>(this, "RefreshData", (sender) =>
{
    // Refresh your data
    LoadData();
});
Using with ViewModels
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);
        });
    }
}

3. Important: Unsubscribing

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();
    }
}

4. Sender Filtering

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

API Usage

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.

Important Behavioral Differences

⚠️ This implementation has stricter subscription behavior than the original .NET MAUI MessagingCenter:

  • 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.

Acknowledgements

The bubble icon has kindly been provided by Smashicons

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-android35.0 is compatible.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-ios18.0 is compatible.  net9.0-maccatalyst was computed.  net9.0-maccatalyst18.0 is compatible.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net9.0-windows10.0.19041 is compatible.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 148 5/29/2025
1.0.0-preview1 12,226 7/10/2024