简体   繁体   中英

mvvmcross IOS: How to callback from a ViewModel to a View

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");
    }

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();
    }

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.

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)

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

To implement anything perfectly, you really should add serialisation code to make sure this all works during "tombstoning" on all platforms... but often this is overkill - for a simple data collection dialog users often don't need "perfect" tombstoning support.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM