Returning values to 'parent' viewmodel in

2020-03-03 06:21发布

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.

标签: mvvmcross
1条回答
趁早两清
2楼-- · 2020-03-03 06:48

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..
   }
}
查看更多
登录 后发表回答