0

I'm wondering what the best way is to communicate between Controllers in MVVM.

  1. I could work with Dependency Injection so that every Controllers automatically gets a reference to the controllers it like to communicate with.
  2. I could have references in my MainController and every other controller has to ask the MainController for communication.

Anyway I'm also wondering if it is a good idea to communicate between controllers in the first place. Could you provide me any information about this?

Frame91
  • 3,670
  • 8
  • 45
  • 89

2 Answers2

1

If you use MVVM Light you can use the Messaging functionality to communicate between Controllers.

Short overview: a Controller can Register for Messages of a certain type (which might be just a class) and other can send this objects.

See MVVM Light Messenger - Sending and Registering Objects for a sample

Community
  • 1
  • 1
Sascha
  • 10,231
  • 4
  • 41
  • 65
  • Hi, thanks! I'm currently using WAF (http://waf.codeplex.com/) instead of MVVM Light. Do you suggest to implement an own Messaging-Functionality? – Frame91 Mar 29 '14 at 14:38
  • I would not recommend writing your own Messaging Service. It is a hard work (especially as you must consider references that hold otherwise would be garbage collected). Maybe there is already a separate implementation I do not know – Sascha Mar 29 '14 at 17:27
  • Note: there's a blog here: http://slickthought.net/post/2011/01/20/Implementing-MVVM-Message-Routing-with-XAML.aspx – Sascha Mar 29 '14 at 17:29
  • Thanks in advance - do you know any reason against having references of controllers in other controllers? – Frame91 Mar 29 '14 at 17:43
  • You can have references to other Controllers. But: the Problem with Held references still applies to this scenario – Sascha Mar 29 '14 at 18:09
0

You an Event Aggregator is a great way to do it. MVVM Light I believe has one built in, and I know Caliburn.Micro does. But it's also trivial to make your own. This will let you communicate from one ViewModel to many easily and you can mock them for testing purposes easily as well.

Kelly
  • 6,992
  • 12
  • 59
  • 76