将集合保留在MVVM的列表中 [英] Keep the collections in the list in MVVM

查看:216
本文介绍了将集合保留在MVVM的列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我在将集合保存在mvvm的列表中时遇到了麻烦,每当我导航离开并返回页面时,列表就会变得空着.我该如何处理?有人可以帮我弄这个吗? 该模型: 我尚未实施其他项目,因为我无法将项目添加到列表中

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; }

        }

视图模型:

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 });
                   }

} 视图:

.....

 <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>

推荐答案

根据您提供的信息,我可以理解您没有实例化ObservableCollection CartData,但是看起来好像其中CartData设置为null.

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.

鉴于问题的信息有限,我建议您使用PRISM的最佳功能EventAggregator之一.它将解决您的问题.

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可以帮助您发布带有值的Model(CartData)并在需要时订阅.

EventAggregator is something which will help you to Publish the your Model(CartData) with values and Subscribe it as and when you need it.

一些有用的链接

试图了解事件聚合器模式

http://msdn.microsoft.com/en-us/library /ff921122.aspx

这篇关于将集合保留在MVVM的列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆