Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning values to 'parent' viewmodel in MvvmCross

Tags:

mvvmcross

What is the recommended method of passing a parameter from one viewmodel to another, modifying it then returning it to the original viewmodel?

Much has been written about passing values in to views ie ShowViewModel(new{ paramX=valueY} ), however I am unable to find any working examples of having the displayed 'submodel' return a value back to the parent when it is closed/dismissed in some method.

The only sample I've found which seems to cover this is http://www.gregshackles.com/2012/11/returning-results-from-view-models-in-mvvmcross/ however the code doesn't seem to work on the new current v3 mvx, failing at runtime with error resolving the viewmodel type, presumably because reflection in mvx wasn't able to identify/register the type due to subtyping or generics.

like image 633
geoffreys Avatar asked Jun 26 '13 12:06

geoffreys


1 Answers

After discussing with the author of the link from my question, the code does work with one minor tweak and a correction to the name of my View class to conform to the mvvmcross convention.

My view was incorrectly named SomethingViewController rather than SomethingView.

The alteration to Greg's code to work on the current MVX v3 codebase is to change his sample from:

public abstract class SubViewModelBase<TResult> : ViewModelBase 
{
   protected string MessageId { get; private set; }

   protected SubViewModelBase(string messageId)
   {
      MessageId = messageId;
   }
   ....
}

to:

public abstract class SubViewModelBase<TResult> : ViewModelBase 
{
   protected string MessageId { get; private set; }

   public virtual void Init(string messageId){
      this.MessageId = messageId;
   }
}

and of course in your submodels use

public abstract class MySomeModel : SubViewModelBase<YourReturnType> 
{
   public override void Init(string messageId, other.. parameters..){
      base.Init(messageId);
      .. your other parameters init here..
   }
}
like image 129
geoffreys Avatar answered Oct 21 '22 07:10

geoffreys