0
votes

So I've been seaching the web, but with all the updates to MVVMCross I cant figure out how to do this.

Its pretty straight forward to me that I can bind doing the following:

MvxFluentBindingDescriptionSet<PageOneView, PageOneViewModel> set = this.CreateBindingSet<PageOneView, PageOneViewModel>();
set.Bind(_HearingLossPickerView).To(vm => vm.HearingLoss);
set.Apply();

However, this creates a new instance of my viewmodel. I want to be able to use an existing viewmodel. I've tried just setting the DataContext, but no dice.

So essentially my flow looks like this.
ViewController (PagesViewController) -> View (PageOneView)
MainViewModel (PagesViewModel) -> SubViewModel (PageOneViewModel)

I'm able to create the PagesViewModel like I did using the set and I want to essentially pass the PageOneViewModel from PagesViewModel to the PageOneView

Pretty basic stuff so if anyone could help that be amazballs.

2

2 Answers

1
votes

MvvmCross allows you to,override the view model location - see the section and sample code on this in https://github.com/MvvmCross/MvvmCross/wiki/Customising-using-App-and-Setup

Also, you can just set the ViewModel directly in View code - just be sure to do this before base.ViewDidLoad which is where MvvmCross does ViewModel location.


Note that the basic pattern of relying on ViewModels to be pre-created can be problematic in environments like Android and WindowsPhone where tombstoning means that your app can be shutdown and restarted on any Activity or Page.

0
votes

Pretty solid that Stuart responded. Great stuff man. But After watch a few of your videos from the N+1 series you created what I figured out I needed to do was a DelayBinding.

So for anyone else stuck check out N+28 for iOS

Example.

My child ViewModel

this.DelayBind(() =>
{
    MvxFluentBindingDescriptionSet<SomeView, SomeViewModel> set = this.CreateBindingSet<SomeView, SomeViewModel>();
    set.Bind(SubmitButton).To(v => v.NextCommand);
    set.Apply();
});

My parent ViewModel

MvxFluentBindingDescriptionSet<ViewController, ViewControllerViewModel> set = this.CreateBindingSet<ViewController, ViewControllerViewModel>();
set.Bind(SomePage).For(p => p.DataContext).To(v => v.SomeViewModel);