I made a Model Class "RestaurentList" which populates two collections with data from a json file. After instantiating an object to the class in my ViewModel i databind the collections to an ItemsControl.
Everything in the above works fine, but when i call the method populatePartialList from the object in my ViewModel, it doesn't contain any of the data from when i instantiated the object. This means, that when my method tries to repopulate the PartialList, it can't because it doesn't find the data from FullList.
Edit: I left out some of the code as you can see with the comment tags. I just want to give you an understanding of how i am doing this.
My question basicly is, why the object doesn't contain any data when i'm calling the method populatePartialList.
My guess it's got something to do with the fact, that i am databinding the List to an ItemsControl and therefor can't access it anymore? What should do in that case then? I'm trying to make a very simple pagination
Edit to the above; I tried removing my Bind and i still can't reach the data.
Model:
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
}
}
View Model:
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
}
Edit for Jon:
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()
{
}
Look at the line of code before your call to populatePartialList
:
_list = new RestaurentList();
You've created a new instance of RestaurentList
. That will have called populateList()
, but not waited for it complete. Assuming your real implementation of populateList
contains await
operations, that means your call to populatePartialList(2)
will almost certainly occur before the data is ready.
You need to think about how the asynchrony works here, and how you want it to work. Note that while you can't have an asynchronous constructor, you could have an asynchronous static method... that may well be a better idea for both ViewModelDefault
and RestaurentList
.
See more on this question at Stackoverflow