我做了一个模型类“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()
{
}
作为一个侧面说明,餐厅以'ant'而不是'ent'结尾 - 现在是修复名称的好时机。我还强烈建议您遵循.NET命名约定的方法,即'PopulatePartialList'等。 – 2014-10-06 09:27:01