在MvvmCross中将值返回到“父"视图模型 [英] Returning values to 'parent' viewmodel in MvvmCross

查看:65
本文介绍了在MvvmCross中将值返回到“父"视图模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将参数从一个视图模型传递到另一个视图模型,对其进行修改然后将其返回到原始视图模型的推荐方法是什么?

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

关于将值传递到视图即ShowViewModel(new {paramX = valueY})的文章已有很多,但是我无法找到任何有效的示例来使显示的子模型"在返回值时将值返回给父对象.通过某种方法关闭/关闭.

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.

我找到的似乎可以解决这一问题的唯一示例是 http://www.gregshackles.com/2012/11/returning-results-from-view-models-in-mvvmcross/,但是该代码似乎不适用于新的当前v3 mvx,失败在运行时出现错误,导致视图模型类型错误,这大概是因为mvx中的反射由于子类型或泛型而无法识别/注册该类型.

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.

推荐答案

与我的问题的链接作者讨论后,该代码确实进行了一些细微调整,并对我的View类的名称进行了更正以使其符合mvvmcross约定.

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.

我的视图被错误地命名为SomethingViewController而不是SomethingView.

My view was incorrectly named SomethingViewController rather than SomethingView.

为了在当前MVX v3代码库上工作而对Greg的代码进行的更改是将其示例从以下位置更改:

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;
   }
   ....
}

收件人:

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..
   }
}

这篇关于在MvvmCross中将值返回到“父"视图模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆