问题
currently im having troubles in keeping the collections in the list in the mvvm, every time that i navigate away and return to a page, the list keeps getting empty. how am i going to deal with this? can someone help me with this? The Model: I have not implemented the others yet because i cant add items to the list
class CartData
{
public int Cakeprice { get; set; }
public ImageSource ImagePath { get; set; }
public string Caketype { get; set; }
public string Cakename { get; set; }
public int TotalItems { get; set; }
}
The View Model:
class CartingDataSource : BindableBase
{
public ObservableCollection<CartData> _cartData;
public ObservableCollection<CartData> CartData
{
get {
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 });
}
} The View:
.....
<Page.DataContext>
<vm:CartingDataSource/>
</Page.DataContext>
....
<ListView
x:Name="itemListView"
AutomationProperties.AutomationId="ItemsListView"
AutomationProperties.Name="Items"
TabIndex="1"
Margin="-10,130,0,264"
Padding="120,0,0,60"
ItemsSource="{Binding cartData}"
IsSwipeEnabled="False" Grid.RowSpan="2" ItemClick="itemListView_ItemClick" SelectionChanged="itemListView_SelectionChanged_1" IsItemClickEnabled="True">
<ListView.ItemTemplate>
<DataTemplate>
<Grid Margin="6">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Border Background="{ThemeResource ListViewItemPlaceholderBackgroundThemeBrush}" Width="60" Height="60">
<Image Stretch="UniformToFill" AutomationProperties.Name="{Binding Title}" Source="Assets/TempPic.jpg"/>
</Border>
<StackPanel Grid.Column="1" Margin="10,0,0,0">
<TextBlock Text="{Binding Cakename}" Style="{StaticResource TitleTextBlockStyle}" TextWrapping="NoWrap" MaxHeight="40"/>
<TextBlock Text="{Binding Cakeprice}" Style="{StaticResource CaptionTextBlockStyle}" TextWrapping="NoWrap"/>
</StackPanel>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemContainerStyle>
<Style TargetType="FrameworkElement">
<Setter Property="Margin" Value="0,0,0,10"/>
</Style>
</ListView.ItemContainerStyle>
</ListView>
回答1:
When are you creating an instance of the collection?
I suggest you make the collection readonly and create an instance only when needed, of the backing store field.
public ObservableCollection<CartData> CartData
{
get {
if( _cartData == null ) _cartData = new ObservableCollection<CartData> ();
return _cartData;
}
set {
SetProperty(ref _cartData, value);
}
}
回答2:
Form the information you have provided I can understand that you are not instantiating the ObservableCollection CartData but, it looks like some where CartData is getting set to null.
With limited information about question I wold suggest you to use PRISM's one of the best feature EventAggregator. It will solve your problem.
EventAggregator is something which will help you to Publish the your Model(CartData) with values and Subscribe it as and when you need it.
Some helpful Links
Trying to understand the event aggregator pattern
http://msdn.microsoft.com/en-us/library/ff921122.aspx
来源:https://stackoverflow.com/questions/22189517/keep-the-collections-in-the-list-in-mvvm