mvvmcross IOS:如何从ViewModel回调到View [英] mvvmcross IOS: How to callback from a ViewModel to a View

查看:337
本文介绍了mvvmcross IOS:如何从ViewModel回调到View的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MvxViewController,在ViewDidLoad中,我将按钮绑定到视图模型.单击按钮后,我将打开另一个视图,在该视图中,我需要将字符串返回到我的第一个视图

I have a MvxViewController and in the ViewDidLoad i bind the button click to the viewmodel. When the button is clicked I open another view in which I will need to return a string back to my first view

    public override void ViewDidLoad ()
    {
        var set = this.CreateBindingSet<MyView1, MyView1ViewModel>();
        set.Bind(myButton).To(vm => vm.MyButtonCommand);
        set.Apply();
    }

    public ICommand MyButtonCommand
    {
        get
        {
            _myButtonCommand = _myButtonCommand ?? new MvxCommand(MyButtonCommandClick);
            return _myButtonCommand;
        }
    }
    private void MyButtonCommandClick()
    {
        ShowViewModel<ViewModelNumber2>();
    }

在第二个视图中运行了一些逻辑之后,我想返回字符串

After some logic is ran in my second view I want to return the string

    private void SomeMethodInViewModelNumber2()
    {
        //Raise event that will get pickup up in MyView
        //Or somehow get "SomeString"
        if (OnMyResult != null)
            OnMyResult ("SomeString");
    }

问题是我不想使用Messenger发送回字符串.我有我的理由,但基本上是因为ViewModelNumber2可以从许多不同的地方打开,并且工作原理略有不同,并且管理需要发送回的不同消息以及在何处订阅这些消息会很麻烦

The problem is that I don't want to send the string back using the messenger. I have my reasons but basically because ViewModelNumber2 can be opened from many different places and works slightly different and managing the different messages that would need to be sent back and where to subscribe to these messages would be a mess

有什么办法可以做下面的事情吗?

Is there any way that I can do something like the below?

    public override void ViewDidLoad ()
    {
        var set = this.CreateBindingSet<MyView1, MyView1ViewModel>();
        set.Bind(myButton).To(vm => vm.MyButtonCommand).OnMyResult((myString) => {Process(myString)});
        set.Apply();
    }

或者也许当我创建ViewModelNumber2时,我应该将callBack传递给构造函数,并使用该回调将字符串从ViewModelNumber2发送回MyView1ViewModel

Or perhaps when I create ViewModelNumber2 I should pass a callBack into the constructor and use that to send the string back from ViewModelNumber2 to MyView1ViewModel

ShowViewModel<ViewModelNumber2>(OnMyResult);

做到这一点的最佳方法是什么?

What is the best way to do this?

推荐答案

总之:我不知道什么是最好的方法".

In short: I don't know what "the best way to do this" is.

ChildViewModel-ParentViewModel消息的区域很复杂-尤其是因为在使用活动的Android和使用Pages的WindowsPhone的平台上,您无法保证在显示Child时ParentViewModel将在内存中. (注意:这在iOS上不是问题,因为它的应用暂停"模型更简单)

The area of ChildViewModel-ParentViewModel messages is complicated - especially because on platforms like Android using Activities and WindowsPhone using Pages you have no guarantee that the ParentViewModel will be in memory when the Child is shown. (Note: this isn't a problem on iOS as its "app suspension" model is simpler)

当我确实需要一个ViewModel将数据返回到另一个ViewModel时,

When I do need one ViewModel returning data to another, then:

  • 通常,我会尝试将数据收集视图实现为弹出对话框"而不是整个页面",这使父子ViewModel关系更加正确-并确保当孩子关闭.

  • Often I try to implement the data collection views as "popup dialogs" rather than as "whole pages" - this makes the parent-child ViewModel relationship more correct - and ensures the parent ViewModel will be in memory when the child closes.

通常,我建议人们使用像Greg所述的基于Messenger的技术,该技术在

Often I recommend people use a Messenger-based technique like Greg describes in: http://www.gregshackles.com/2012/11/returning-results-from-view-models-in-mvvmcross/

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