-1

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 request and I am getting response as expected. This works exactly how I want it to work. But the problem is, I can seem to show that response in my fragmentB.axml page (someNumber and status). Can anyone help me out with this problem. Thanks!!

ViewmodelA.cs:

    public MvxCommand SomeCommand
    {
        get
        {
            return new MvxCommand(() => something());
        }
    }
    public async void something()
    {
        ShowViewModel<ViewModelB>();

        SomeService serviceWrapper = new SomeService();
        var model = {//Some Json request};
        var result = await serviceWrapper.SubmitRequestAsync(model);
        SomeResponse response = StaticMethods.DeserializeJson<SomeResponse>(result);

        Status = response.SomeResponse1.Activity[0].Status.Description;
        SomeNumber = response.SomeResponse1.SomeNumber;

        Debug.WriteLine("SomeNumber : " + SomeNumber );
        Debug.WriteLine("Status: " + Status);

    }

    private string _someNumber;
    public string SomeNumber 
    {
        get
        {
            return _someNumber;
        }

        set
        {
            SetProperty(ref _someNumber, value);
            RaisePropertyChanged(() => SomeNumber);
        }
    }

    private string _status;
    public string Status
    {
        get
        {
            return _status;
        }

        set
        {
            SetProperty(ref _status, value);
            RaisePropertyChanged(() => Status);
        }
    }

fragmentA.axml

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Submit"
    android:id="@+id/Submit"
    local:MvxBind="Click SomeCommand" />

fragmentB.axml

<TextView
    android:text="Some Number:"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/SomeNum"
    local:MvxBind="Text SomeNumber "/>
<TextView
    android:text="Status:"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/status"
    local:MvxBind="Text Status"/>
Nick King
  • 190
  • 3
  • 20
  • Does your class defined :INotifyPropertyChanged? You didn't provide class definition – Yuri S Jun 20 '17 at 17:56
  • 1
    I'm a little bit confused. It seems like you are trying to navigate to ViewModelB, but the properties you want to show in your view is in ViewModelA? – Ulbo Jun 20 '17 at 17:59
  • @Ulbo I see what you are saying but I want to send the http request, when I click on the button. But my problem is that I don't know how to display that data in another viewmodel's fragment (i.e. fragmentB). If you know how can I approach this problem, please let me know. Thanks – Nick King Jun 20 '17 at 18:28

2 Answers2

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 = await GetSomeData();
ShowViewModel<ViewModelB>(new { status = Status, number = SomeNumber });

Then in ViewModelB:

public void Init(string status, string number)
{
    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:

public class MyService : IMyService
{
    public string Status {get; set;}
    public string Number {get; set;}

    public async Task DoStuff()
    {
    }
}

Then in ViewModelA ctor would be:

public ViewModelA(IMyService service)

In your Command:

public async void something()
{
    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:

 public string Status => _service.Status;

These are just two ways of solving this problem.

Cheesebaron
  • 24,131
  • 15
  • 66
  • 118
0

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.

Ulbo
  • 346
  • 8
  • 20
  • For the second option, how can I send the data in ViewModelA. Can you give an example? – Nick King Jun 20 '17 at 19:13
  • 1
    Stuart has given a great answer on how to pass parameters between viewmodels. You should go and check it out here: https://stackoverflow.com/questions/19058173/passing-complex-navigation-parameters-with-mvvmcross-showviewmodel – Ulbo Jun 20 '17 at 20:00
  • 1
    See the official documentation on passing parameters: https://www.mvvmcross.com/documentation/fundamentals/navigation – Martijn00 Jun 20 '17 at 20:23