Skip to content Skip to sidebar Skip to footer

Mvvmcross - Mvxbind Not Bind Properly

In my viewmodelA, I have a property that when the button from my fragmentA.axml is clicked, I do Mvxbind and the screen changes and it shows viewmodelB and also I send an http req

Solution 1:

MvvmCross does not do one ViewModel to n Views. Only 1:1 relationships are allowed.

There are various ways to tackle your problem.

1. Pass along an object in ShowViewModel or the new NavigationService which describes your result from ICommand. For this to work, you need to wait navigating until your request is done:

var result = awaitGetSomeData();
ShowViewModel<ViewModelB>(new { status = Status, number = SomeNumber });

Then in ViewModelB:

publicvoidInit(string status, stringnumber)
{
    Status = status;
    Number = number;
}

Then have props for Status and Number in that ViewModel.

2. Have a Service that you share between your ViewModels and have it keep the state and take care of your rest calls:

publicclassMyService : IMyService
{
    publicstring Status {get; set;}
    publicstring Number {get; set;}

    publicasync Task DoStuff()
    {
    }
}

Then in ViewModelA ctor would be:

publicViewModelA(IMyService service)

In your Command:

publicasyncvoidsomething()
{
    await _service.DoSomething();
    ShowViewModel<ViewModelB>();
}

Ctor in ViewModelB would be similar to ViewModelA and just populate whatever props or have the props directly reflect what is in service like:

publicstring Status => _service.Status;

These are just two ways of solving this problem.

Solution 2:

As far as I can see you got two options:

First option is to wait to send you http call until you are in ViewModelB, and load the data over there.

Second option is to wait until your http call has finished before navigating, and sending the data fetched in ViewModelA as a navigation parameter for ViewModelB.

Post a Comment for "Mvvmcross - Mvxbind Not Bind Properly"