2017-03-08 46 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 
} 

希望這個答案可以幫助。

相關問題