Xamarin.Forms MessagingCenter

Introduction:

MessagingCenter is one of the most important APIs that you should be aware of while working with Xamarin.Forms and MVVM. MessagingCenter includes a simple messaging service to send and receive messages.

Please refer to this amazing document by Microsoft, which will run you through some basics, some of which I’ve included in this post in detail:

Overview

Xamarin.Forms MessagingCenter enables VM’s and other components to communicate without having to know anything about each other besides a simple messaging contract. The goal of MVVM is to abstract your Views from your Business Logic. I believe this ensures both reusability and testability for code. Different MVVM Frameworks offer tools to enhance the above. These are built into Xamarin.Forms, but one feature that most developers forget about is the MessagingCenter. Its entire goal is to enable ViewModels or other components to communicate with each other without having to know anything about each other besides a simple message contract.

Working:

There are APIs that are exposed by MessagingCenter:

  • Subscribe — Listens for messages with a certain signature and performs an action when they are received. (Multiple subscribers can be listening for the same message.)
  • Send — Publishes a message for listeners to enact upon. If no listeners have subscribed, then the message is ignored.

The MessagingCenter is a static class with Subscribe and Send methods that are used throughout the solution.

Messages have a string message as a parameter that’s used as a way to address messages. The Subscribe and Send methods use generic parameters to further control how messages are delivered —two messages with the same message text but different generic type arguments will not be delivered to the same subscriber.

Using the MessagingCenter

Messages may be sent as a result of a user interaction (like a button click), a system event (like controls changing state) or some other event (like an asynchronous download completing).

Subscribers might be listening to change the appearance of the UI, save data, or trigger some other operation.

For more information about using the MessagingCenter class, see Communicating Between Loosely Coupled Components.

Simple String Message

The simplest message contains just a string in the message parameter. A Subscribe method that listens for a simple string message as shown below—notice the generic type specifying the sender is expected to be of type MainPage. Any classes in the solution can subscribe to the message using this syntax:

In the MainPage class, the following code sends the message. The this parameter is an instance of MainPage.

The string doesn’t change — it indicates the message type and is used for determining which subscribers to notify. This sort of message is used to indicate that an event occurred, such as “upload completed”, where no further information is required.

Passing an Argument

To pass an argument with the message, specify the argument Type in the Subscribe generic arguments and in the Action signature.

To send the message with an argument, including the Type generic parameter and the value of the argument in the Send method call.

This simple example uses a string argument but any C# object can be passed.

There is an amazing blog by James Montemagno where he has shown in-depth how to use the MessagingCenter API’s.

Unsubscribe

An object can unsubscribe from a message signature so that no future messages are delivered. The Unsubscribe method syntax should reflect the signature of the message (as such may need to include the generic Type parameter for the message argument).

Summary

MessagingCenter is a simple way to reduce coupling, especially between view models. It can be used to send and receive simple messages or pass an argument between classes. Classes should unsubscribe from messages they no longer wish to receive.

If I’ve missed something, go ahead and add it in the comments. I’ll make sure to add any needed changes to the post. Also, if you find something incorrect in the blog, please go ahead and correct me in the comments.

Smash that clap button if you liked this post.

You can reach out to me on LinkedIn or StackOverflow! I am always available! 😛

Fritz

Our team has been at the forefront of Artificial Intelligence and Machine Learning research for more than 15 years and we're using our collective intelligence to help others learn, understand and grow using these new technologies in ethical and sustainable ways.

Comments 0 Responses

Leave a Reply

Your email address will not be published. Required fields are marked *

wix banner square