2014-10-06 107 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

相关问题