2017-03-08 70 views
0
背面

我有以下简单的导航流量: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线程中。

+0

你能提供一些示例代码吗?或者你可以看看这个[Stackoverflow的答案](http://stackoverflow.com/questions/42441831/mvvmcross-how-to-raisepropertychange-from-another-viewmodel/42447761#answer-42447761)的一些其他想法刷新数据通过ViewModels。 – Plac3Hold3r

+0

@ Plac3Hold3r我提供了示例代码。我接受其他解决方案。我想过简单的数据库来存储产品,但它似乎是一个不好的解决方案。 – straiser

+0

简而言之,你的意思是当'ViewModel3'关闭时你想从'ViewModel3'向'ViewModel1'添加数据? –

回答

0

您可以在静态字段中添加一个GlobalVars.cs静态类(可能会创建Services文件夹并将其放在里面,这样会更干净),并将它用作ViewModel1中列表的数据源。然后,您需要做的就是从ViewModel3List,GlobalVars.cs中添加数据。

服务/ GlobalVars.cs

public static class GlobalVars 
{ 
    public static List<CustomClass> aGlobalVariable; 
} 

ViewModel1.cs

using Services; 
... 
public CustomClass LocalVariable 
{ 
    get { return GlobalVars.aGlobalVariable; } 
    set 
    { 
     GlobalVars.aGlobalVariable = value; 
     ... 
    } 
} 

//refresh purpose 
public void refresh() 
{ 
    RaisePropertyChanged(() => LocalVariable); 
} 

ViewModel3.cs

//just add the GlobalVars List when you need it 
GlobalVars.aGlobalVariable.Add(new CustomClass()); 

要刷新您的ListView,当您回到ViewModel1时,您只需从活动中调用refresh()方法即可。以下是您如何访问ViewModel的活动。

View1.cs

ViewModel1 vm; 
... 
protected override void OnCreate(Bundle bundle) 
{ 
    base.OnCreate(bundle); 
    ... 
    vm = ViewModel as ViewModel1; 
    ... 
} 
//what you want to do is refreshing the list on resume 
protected override void OnResume() 
{ 
    base.OnResume() 
    vm.refresh(); 
    ... //other things you might want to do 
} 

希望这个答案可以帮助。

相关问题