Log In
Home
Support
Demos
Documentation
Blogs
Training
Webinars
[Expand]General Information
[Collapse]WinForms Controls
  Prerequisites
 [Expand]What's Installed
 [Collapse]Build an Application
  [Expand]Choose Application UI
   Data Management Controls
  [Expand]Printing and Exporting
  [Collapse]WinForms MVVM
   [Collapse]Concepts
     Conventions and Attributes
     Fluent API Support
     Data Bindings and Notifications
     Commands
     Services
     Behaviors
     Layer Communication. Messenger
     View Management
     ViewModel Management
   [Expand]Design-time Support
   [Expand]Tutorials
  [Expand]Skins
  [Expand]Localization
  [Expand]Right-to-Left Layout
   Redistribution and Deployment
 [Expand]Controls and Libraries
 [Expand]Common Features
  Get More Help
 [Expand]API Reference
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]ASP.NET Core Bootstrap Controls
[Expand]WPF Controls
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Document Server
[Expand]Reporting
[Expand]Report Server
[Expand]Dashboard
[Expand]eXpressApp Framework
[Expand]CodeRush
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation

Layer Communication. Messenger

MVVM Best Practices Demo

The text below has a related example in the DevExpress 'MVVM Best Practices' demo.

Group: API Code Examples
Module: Messenger
Example: Messenger
17.2 Demo Center: Launch the demo

Your real-life applications will almost always have multiple Views and ViewModels. And, with rare exceptions,you will need a way for these separate layers to communicate with each other and share data. To solve this task, use the DevExpress MVVM Messenger.

The idea of this message mechanism is very simple: in the sender ViewModel, you call the Send method to transmit required data as a message. In the recipient ViewModel, the Register method is called to catch all messages.

The Register method establishes a permanent connection between your ViewModels. This means once called, the Register method does not need to be called ever again - all consequent messages sent will be received automatically. To break this connection, use the Unregister method.

If there are multiple senders that transmit messages of the same type, you can use tokens to label messages sent from different senders. Tokens can be objects of any type, the code below uses simple string tokens.

The Register and Unregister methods have corresponding overloads that allow you to receive or block messages marked with the specific token.

How would you rate this topic?​​​​​​​