2014-10-06 44 views
0

我做了一個模型類「RestaurentList」,它使用json文件中的數據填充兩個集合。 在將ViewModel中的對象實例化到類後,我將數據綁定到ItemsControl。當通過方法訪問時,對象沒有保存實例

上述所有的工作都很好,但是當我從我的ViewModel中的對象調用方法populatePartialList時,它不包含任何來自於我實例化對象的數據。 這意味着,當我的方法試圖重新填充PartialList時,它不能因爲它沒有從FullList中找到數據。

編輯:我遺漏了一些代碼,你可以看到與評論標籤。 我只是想讓你瞭解我是如何做到這一點的。

我的問題基本上是,當我調用方法populatePartialList時,爲什麼對象不包含任何數據。

我的猜測是這樣的事實,我將數據綁定到一個ItemsControl,因此無法再訪問它?那麼該怎麼辦呢?我試圖做一個非常簡單的分頁

編輯到上面;我嘗試刪除我的綁定,我仍然無法訪問數據。

型號:

public class RestaurentList 
    { 
     private ObservableCollection<Restaurent> _fullList = new ObservableCollection<Restaurent>(); 

     private ObservableCollection<Restaurent> _partialList = new ObservableCollection<Restaurent>(); 

     public ObservableCollection<Restaurent> FullList 
     { 
      get { return _fullList; } 
     } 

     public ObservableCollection<Restaurent> PartialList 
     { 
      get { return _partialList; } 
     } 

     public RestaurentList() 
     { 
      populateList(); 
     } 

     public void populatePartialList(int fromValue = 1) 
     { 
      int collectionAmount = _fullList.Count; 
      int itemsToShow = 2; 
      fromValue = (fromValue > collectionAmount ? 1 : fromValue); 

      foreach (Restaurent currentRestaurent in _fullList) 
      { 
       int currentId = Convert.ToInt32(currentRestaurent.UniqueId); 
       if (currentId == fromValue || (currentId > fromValue && currentId <= (fromValue + itemsToShow)-1)) 
       { 
        _partialList.Add(currentRestaurent); 
       } 
      } 
     } 

     private async void populateList() 
     { 
      // Get json data 

      foreach (JsonValue restaurentValue in jsonArray) 
      { 
       // populate full list 

       foreach (JsonValue menuValue in restaurentObject["Menu"].GetArray()) 
       { 
        // populate full list 
       } 
       this._fullList.Add(restaurent); 
      } 
      populatePartialList(); 
     } 

     public override string ToString() 
     { 
      // Code 
     } 
    } 

視圖模型:

class ViewModelDefault : INotifyPropertyChanged 
    { 


     private RestaurentList _list; 

     public ObservableCollection<Restaurent> List 
     { 
      get { return _list.PartialList; } 
     } 


     public ViewModelDefault() 
     { 
      _list = new RestaurentList(); 
      _list.populatePartialList(2); // This is where i don't see the data from RestaurentList 
     } 

     #region Notify 
    } 

編輯喬恩:

public RestaurentList() 
     { 
      PopulatePartialList(); 
     } 

     public async void PopulatePartialList(int fromValue = 1) 
     { 
      await PopulateList(); 
      int collectionAmount = _fullList.Count; 
      int itemsToShow = 2; 
      fromValue = (fromValue > collectionAmount ? 1 : fromValue); 

      foreach (Restaurent currentRestaurent in _fullList) 
      { 
       int currentId = Convert.ToInt32(currentRestaurent.UniqueId); 
       if (currentId == fromValue || (currentId > fromValue && currentId <= (fromValue + itemsToShow)-1)) 
       { 
        _partialList.Add(currentRestaurent); 
       } 
      } 
     } 

     private async Task PopulateList() 
     { 
} 
+1

作爲一個側面說明,餐廳以'ant'而不是'ent'結尾 - 現在是修復名稱的好時機。我還強烈建議您遵循.NET命名約定的方法,即'PopulatePartialList'等。 – 2014-10-06 09:27:01

回答

0

看的代碼的調用之前的行populatePartialList

_list = new RestaurentList(); 

您已創建RestaurentList的新實例。這將稱爲populateList(),但而不是等待它完成。假設您的populateList的實際實施包含await操作,這意味着您在populatePartialList(2)的調用幾乎肯定會在數據準備就緒之前發生。

您需要思考異步如何在這裏工作,以及如何它的工作。請注意,儘管您不能擁有異步構造函數,但您可以使用異步靜態方法...對於ViewModelDefaultRestaurentList而言,這可能是一個更好的主意。

+0

這很有道理。我只是在學習異步,我很抱歉,如果這可能太明顯了。 – Mathias 2014-10-06 09:30:56

+0

@Mathias:在異步中很少是顯而易見的;)期待它需要一段時間讓你的頭,說實話... – 2014-10-06 09:31:35

+0

呵呵。謝謝你的提示。 我必須確定我明白在這裏要做什麼。 如果我讓我的populatePartialList工作異步,我如何讓它等待首先填充的FullList? – Mathias 2014-10-06 09:36:04

相關問題