我做了一個模型類「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