Issue
Consider the following example. I have three view models, ViewModel_A
, ViewModel_B
, and ViewModel_Values
.
I want to be able to navigate to ViewModel_Values
from either ViewModel_A
or ViewModel_B
, select a value from ViewModel_Values
, then return that value to the calling view model.
Is there a way of passing arguments to previous view models in the navigation stack so that I can simply call ViewModel_Values.Close(this)
, thereby ensuring that the ViewModels_Values
is decoupled from any other view models and can be used with arbitrary "parent" view models?
Solution
Installing & using the MvxMessenger plugin is a great way to decouple view model communication in MvvmCross -
In your case, you could set up a new message -
public class ValuesChangedMessage : MvxMessage
{
public ValuesChangedMessage(object sender, int valuea, string valueb)
: base(sender)
{
Valuea = valuea;
Valueb = valueb;
}
public int Valuea { get; private set; }
public string Valueb { get; private set; }
}
In ViewModel_Values, you would act on / publish your UX changes with -
_mvxMessenger.Publish<ValuesChangedMessage>(new ValuesChangedMessage(this, 1, "boo!"));
And in ViewModel_A, ViewModel_B you would subscribe and act on them (as your ViewModel A / B would be still in the navigation stack when you pushed ViewModel_Values from them, so they could receive the message) -
private MvxSubscriptionToken _messageToken;
_messageToken = _mvxMessenger.Subscribe<ValuesChangedMessage>(async message =>
{
// use message.Valuea etc ..
});
More infos here -
https://www.mvvmcross.com/documentation/plugins/messenger?scroll=644 https://www.youtube.com/watch?feature=player_embedded&v=HQdvrWWzkIk
Answered By - WickedW
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.