MvvmCross - MvxBind not bind properly

好久不见. 提交于 2020-02-08 11:41:06

问题


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"/>

回答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.




回答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.



来源:https://stackoverflow.com/questions/44659863/mvvmcross-mvxbind-not-bind-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!