Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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"/>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
203 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...