[Expand]General Information
[Expand]WinForms Controls
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]ASP.NET Core Bootstrap Controls
[Collapse]WPF Controls
  Prerequisites
 [Expand]What's Installed
 [Expand].NET Core 3 Support
 [Expand]Common Concepts
 [Collapse]MVVM Framework
  [Expand]View Models
  [Expand]Commands
  [Expand]Behaviors
  [Expand]Services
  [Expand]DXBinding
  [Expand]MIF
   Converters
   ViewLocator
   Messenger
   Data Annotation Attributes
   LayoutTreeHelper
   Weak Event
 [Expand]Controls and Libraries
 [Expand]Scaffolding Wizard
 [Expand]Localization
  Redistribution and Deployment
  Get More Help
 [Expand]API Reference
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Office File API
[Expand]Reporting
[Expand]Report and Dashboard Server
[Expand]Dashboard
[Expand]eXpressApp Framework
[Expand]eXpress Persistent Objects
[Expand]CodeRush
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation
View this topic on docs.devexpress.com (Learn more)

Messenger

The Messenger class allows you to implement a message exchange between modules.

The Messenger class implements the IMessenger interface.

Expanded Getting Started With Messenger

The following step-by-step tutorial illustrates how to use the Messenger to send notifications from one module (ViewModel) to another:

  1. Create a Sender and Receiver View and ViewModel.

  2. Implement a method that sends messages (SendMessage in a code sample below). Bind the Send Message button within the Sender View to the method.

  3. Enable the Receiver View Model to get messages. The ViewModel's ReceivedMessage property value is set to the message text.

The animation below illustrates the result.

Expanded Default Messenger

The static Messenger.Default property returns a default Messenger instance. The default messenger is not multi-thread safe and stores weak references.

Change the Default Messenger

To change the Default messenger, create a new Messenger instance and pass is to the static Messenger.Default property. For example, the following code replaces the Default Messenger with a multi-thread safe Messenger:

Expanded Send and Receive Messages

Use the following methods to send and receive messages:

The code sample below demonstrates how to send and receive messages of different types.

Send and Receive Message Descendants

When you subscribe to a message of a custom type, you can invoke your handler if a message descendant is received.

Use the following methods to work with the messages of custom inherited types.

The code sample below demonstrates how to invoke a handler when a message descendant is received.

Messages With Tokens

You can invoke message handlers when a message with a particular token is received. Use the following methods to work with messages with tokens:

The code sample below demonstrates how to invoke handlers when a message with an appropriate token is received.

Expanded Unregister Message Handlers

Messenger With Weak References

You do not need to unregister message handlers if you use a Messenger with weak references (for example, the Default Messenger). Subscribing to messengers with weak references does not cause memory leaks.

Messenger With Strong References

You can use a Messenger instance that works with strong references:

To avoid memory leaks, unregister message handlers with any of the following methods:

Expanded Closures in Message Handlers

The weak reference messenger (like the Default messenger) imposes a limitation for lambda expressions with outer variables (closures).

The weak reference messenger refers to a lambda expression with a weak reference. If the garbage collector collects the lambda expression object, the message handler is not invoked.

In the code below, the lambda method refers to the text variable that is defined outside the lambda. In this case, this lambda can be collected and never called.

To protect a lambda expression object from being collected, declare the text variable as a property at the subscriber object level, for example:

You can also store the message handler as follows:

If your lambda expression does not use outer variables, your message handler is invoked without any limitations.

Is this topic helpful?​​​​​​​