4

I am new to MvvmCross and Xamarin. I have been looking into this for some time now and I am trying to find what is the best way to send some data from ViewModel B to ViewModel A. Meaning that ViewModel A is responsible for showing ViewModel B. It's fairly straight forward on how to send data to a ViewModel when launching it, however there is no clearly defined tutorial that I have come across that is showcasing how to send the data back to the starting ViewModel on finishing.

I have come across Event Aggregators like MvvmCross.Messenger that seems to be an ideal candidate. However for an Android project I am not sure if that is a good choice due to Android Activity life cycle methods.

Any help on this would be very well appreciated. Thank you.

Stack Player
  • 1,470
  • 2
  • 18
  • 32
ahmad
  • 2,149
  • 4
  • 21
  • 38

2 Answers2

5

The Messenger is the right way to do it, it has been covered in another stack overflow question. There is even a sample code you can toy with.

The gist is that both ViewModel receive a (possibly singleton) Messenger, and when ViewModelB wants to let ViewModelA it needs to reload its data, ViewModelB sends a message through the messenger. Internally Messenger uses a WeakReference to ensure garbage collection can still go on (check this post for more information)

Community
  • 1
  • 1
Julien Lebot
  • 3,092
  • 20
  • 32
  • Thank you so much, I was also looking at Stuart Lodge's videos. N=12 cover's this in detail. For anyone else looking into this as well for reference, it's an excellent resource. https://www.youtube.com/watch?v=6QCIoIw_O4I&list=PLR6WI6W1JdeYSXLbm58jwAKYT7RQR31-W&feature=player_detailpage&t=115s – ahmad Aug 23 '15 at 07:34
2

It sounds like what you want to do is to show a VM for a particular result to be returned to the "parent" VM. This is baked into Android with StartActivityForResult, but needs some hacking to implement with MvvmCross.

Greg Shackles wrote a tutorial on how this can be accomplished. Further discussion here. It's a better fit for the Android activity flow than using the messenger, if I understand your usecase correctly.

Community
  • 1
  • 1
tempy
  • 1,567
  • 2
  • 18
  • 28
  • I looked at Greg Shackles implementation, he himself has admitted to it partially working. My reason for this post was due to confusion that why does Greg need to do that when MvvmCross.Messenger exists. Is that not a suitable implementation? – ahmad Aug 23 '15 at 09:59
  • Messenger is part of the puzzle, but on it's own it does nothing to give you the desired "modal" behavior wherein a VM opens, collects the input it needs, then closes itself and returns either a result, an error, or a cancel. All it gives you is the capability to send a message, nothing more. – tempy Aug 23 '15 at 10:40
  • Yeah I looked into this, of course the Messenger itself does nothing. However with an implementation provided for it to send a Message from one ViewModel to another, I still can't seem to understand why Greg's implementation is suggested when a Messenger can be used for the same purpose effectively. – ahmad Aug 24 '15 at 15:41