我有以下简单的导航流量:MvvmCross在从另一视图模型
ViewModel1 => ViewModel2 => ViewModel3
当ViewModel3被关闭时,我发布使用消息插件来视图模型一些需要在ViewModel1中添加到列表中的信息。不幸的是没有任何反应(我提出NotifChanged)。在我看来,这是因为它不从UI调用。
实现刷新列表的最佳方式是什么?当ViewModel从另一个ViewModel返回时,即ViewModel3关闭时,ViewModel中没有看到任何方法。
编辑: 示例代码:
public class WarehouseInViewModel : MvxViewModel
{
public WarehouseInViewModel(IMvxMessenger messenger)
{
mvxMessenger = messenger;
myToken = mvxMessenger.Subscribe<mAcceptMessage>(OnMyMessageArrived);
}
public override void Start()
{
base.Start();
}
private readonly IMvxMessenger mvxMessenger;
private MvxSubscriptionToken myToken;
private List<mProduct> productItems;
public List<mProduct> ProductItems
{
get { return productItems; }
set
{
productItems = value;
RaisePropertyChanged(() => ProductItems);
}
}
private MvxCommand<AcceptMenuItem> buttonCommand;
public ICommand ButtonCommand
{
get
{
return buttonCommand = buttonCommand ?? new MvxCommand<AcceptMenuItem>(MenuClick);
}
}
private void OnMyMessageArrived(mAcceptMessage myMessage)
{
mProduct product = mProduct.GetById(myMessage.ProductId);
//Something more ...
// There I want to update my Listview which is binded to ProductItems
ProductItems.Add(product);
RaisePropertyChanged(() => ProductItems);
}
public async void MenuClick(AcceptMenuItem menu)
{
ShowViewModel<WarehouseInScanViewModel>();
}
}
而且从三视图模型的最重要的方法(我们假设secont视图模型只打开第三个,它的正确关闭)):所以
public void ButtonNextClick()
{
vxMessenger.Publish(new mAcceptMessage(this, productId, scannedLocation.Id, productQuantity));
Close(this);
}
,当我从第三个VM返回时,我想重新刷新ListView。我无法从OnMyMessageArrived中完成,因为它不在UI线程中。
你能提供一些示例代码吗?或者你可以看看这个[Stackoverflow的答案](http://stackoverflow.com/questions/42441831/mvvmcross-how-to-raisepropertychange-from-another-viewmodel/42447761#answer-42447761)的一些其他想法刷新数据通过ViewModels。 – Plac3Hold3r
@ Plac3Hold3r我提供了示例代码。我接受其他解决方案。我想过简单的数据库来存储产品,但它似乎是一个不好的解决方案。 – straiser
简而言之,你的意思是当'ViewModel3'关闭时你想从'ViewModel3'向'ViewModel1'添加数据? –