MvvmCross-MvxBind无法正确绑定 [英] MvvmCross - MvxBind not bind properly
问题描述
在我的viewmodelA中,我有一个属性,当我单击fragmentA.axml中的按钮时,我将执行Mvxbind并更改屏幕,并显示viewmodelB,并且我发送了一个http请求,并且得到了预期的响应.这完全可以按照我希望的方式工作.但是问题是,我似乎可以在我的fragmentB.axml页面(someNumber和状态)中显示该响应.谁能帮我解决这个问题.谢谢!!
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"/>
推荐答案
MvvmCross不会对n个视图执行一个ViewModel.只允许1: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. 在ShowViewModel或新的NavigationService中传递一个对象,该对象描述了ICommand的结果.为此,您需要等待导航,直到请求完成:
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 });
然后在ViewModelB中:
Then in ViewModelB:
public void Init(string status, string number)
{
Status = status;
Number = number;
}
然后在该ViewModel中具有用于状态和编号的道具.
Then have props for Status and Number in that ViewModel.
2. 拥有一个在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()
{
}
}
然后在ViewModelA中将是:
Then in ViewModelA ctor would be:
public ViewModelA(IMyService service)
在您的命令中:
public async void something()
{
await _service.DoSomething();
ShowViewModel<ViewModelB>();
}
ViewModelB中的Ctor与ViewModelA类似,只是填充任何道具或使道具直接反映正在使用的东西,例如:
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.
这篇关于MvvmCross-MvxBind无法正确绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!