Answered by:
Maintaining the collection in a List/ObservableCollection

Question
-
i have been successfully adding an item to list in a MVVM, and now my problem is maintaining the list in the view model. Every time i navigate to a page or go back to a page and return to that listview, the list resets. how will i able to achieve that? i am currently using the prism to build the MVVM.
The ViewModel:
public ObservableCollection<CartData> _cartData;
if (_cartData == null)
public ObservableCollection<CartData> CartData
{
get {
{
_cartData = new ObservableCollection<CartData>();
}return _cartData;
}
set {
SetProperty(ref _cartData, value);
}
}
private DelegateCommand _addItemCommand;
public ICommand AddItemCommand
{
get
{
if (_addItemCommand == null)
{
_addItemCommand = new DelegateCommand(AddToCart);
}
return _addItemCommand;
}
}public void AddToCart() {
CartData.Add(new CartData { Cakename = "Black Forest", Cakeprice = 104 });
}
- Edited by hack.luminence Tuesday, March 4, 2014 6:01 AM
Tuesday, March 4, 2014 5:58 AM
Answers
-
You should be persisting the list to a permanent location, then calling it back up. This is the same whether or not you use MVVM.
Matt Small - Microsoft Escalation Engineer - Forum Moderator
If my reply answers your question, please mark this post as answered.
NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.- Marked as answer by Jamles HezModerator Monday, March 10, 2014 8:52 AM
Tuesday, March 4, 2014 9:00 PMModerator -
Two alternatives if you chose not to have an overhead of reading from disk frequently 1) to use a static reference to the data and access this in the VM constructor, or 2) use the IOC pattern by using a dependency injection container and have the data injected into the constructor of the view model. There pros and cons to all three depending on the app.
- Marked as answer by Jamles HezModerator Monday, March 10, 2014 8:52 AM
Tuesday, March 4, 2014 10:06 PM
All replies
-
You should be persisting the list to a permanent location, then calling it back up. This is the same whether or not you use MVVM.
Matt Small - Microsoft Escalation Engineer - Forum Moderator
If my reply answers your question, please mark this post as answered.
NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.- Marked as answer by Jamles HezModerator Monday, March 10, 2014 8:52 AM
Tuesday, March 4, 2014 9:00 PMModerator -
Two alternatives if you chose not to have an overhead of reading from disk frequently 1) to use a static reference to the data and access this in the VM constructor, or 2) use the IOC pattern by using a dependency injection container and have the data injected into the constructor of the view model. There pros and cons to all three depending on the app.
- Marked as answer by Jamles HezModerator Monday, March 10, 2014 8:52 AM
Tuesday, March 4, 2014 10:06 PM