维护收藏列表/的ObservableCollection [英] Maintaining the collection in a List/ObservableCollection
问题描述
我已经成功地将一个项目在MVVM上市,现在我的问题是如何保持在视图模型的列表。每次我浏览一个页面或返回到一个页面,并返回到列表视图,列表重置。我将如何能够做到这一点? 。我目前使用的棱镜来构建MVVM
视图模型:
公开的ObservableCollection< CartData> _cartData;
公众的ObservableCollection< CartData> CartData
{
获得{
返回_cartData;
}
集合{
的SetProperty(REF _cartData,价值);
}
}
私人DelegateCommand _addItemCommand;
公众的ICommand AddItemCommand
{
得到
{
如果(_addItemCommand == NULL)
{
_addItemCommand =新DelegateCommand(AddToCart);
}
返回_addItemCommand;
}
}
公共无效AddToCart(){
CartData.Add(新CartData {Cakename =黑森林,Cakeprice = 104} );
}
查看:
.....
< Page.DataContext>
< VM:CartingDataSource />
< /Page.DataContext>
....
< ListView的
X:NAME =itemListView
AutomationProperties.AutomationId =ItemsListView
AutomationProperties.Name =项目
的TabIndex =1
保证金= - 10,130,0,264
填充=120,0,0,60
的ItemsSource ={结合cartData}
IsSwipeEnabled =FALSEGrid.RowSpan =2项目单击=itemListView_ItemClick的SelectionChanged =itemListView_SelectionChanged_1IsItemClickEnabled =真>
< ListView.ItemTemplate>
<&DataTemplate的GT;
<电网保证金=6>
< Grid.ColumnDefinitions>
< ColumnDefinition WIDTH =自动/>
< ColumnDefinition WIDTH =*/>
< /Grid.ColumnDefinitions>
<边框背景={ThemeResource ListViewItemPlaceholderBackgroundThemeBrush}WIDTH =60HEIGHT =60>
<图像拉伸=UniformToFillAutomationProperties.Name ={结合标题}来源=资产/ wewewew.jpg/>
< /边框>
< StackPanel的Grid.Column =1保证金=10,0,0,0>
< TextBlock的文本={结合Cakename}风格={StaticResource的TitleTextBlockStyle}TextWrapping =NoWrap的了maxHeight =40/>
< TextBlock的文本={结合Cakeprice}风格={StaticResource的CaptionTextBlockStyle}TextWrapping =NoWrap的/>
< / StackPanel的>
< /网格和GT;
< / DataTemplate中>
< /ListView.ItemTemplate>
< ListView.ItemContainerStyle>
<风格的TargetType =FrameworkElement的>
< setter属性=保证金VALUE =0,0,0,10/>
< /样式和GT;
< /ListView.ItemContainerStyle>
< /&的ListView GT;
下面,如果您的视图模型是 CartingDataSource
在这种情况下,它是被instatiated在每一页上的负荷。现在,如果是这样的话,那么你就是在构造函数创建集合的一个新的实例如下:
公共CartingDataSource( ){
CartData =新的ObservableCollection< CartData>();
}
其结果是它重新初始化您的收藏。
您需要从构造函数中删除初始化和做这样的事情:
公开的ObservableCollection< CartData> _cartData;
公众的ObservableCollection< CartData> cartData
{
获得{
如果(_cartData == NULL)
{
_cartData =新的ObservableCollection< CartData>();
}
返回_cartData;
}
集合{
_cartData =价值;
}
}
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;
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 });
}
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/wewewew.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>
Here if your ViewModel is CartingDataSource
in that case it is being instatiated on every page load. Now if that is the case, then you are creating a new instance of your collection in your constructor as below:
public CartingDataSource() {
CartData = new ObservableCollection<CartData>();
}
As a result of which it re-initializes your collection.
You need to remove the initialization from your constructor and do something like this:
public ObservableCollection<CartData> _cartData;
public ObservableCollection<CartData> cartData
{
get {
if(_cartData == null)
{
_cartData = new ObservableCollection<CartData>();
}
return _cartData;
}
set {
_cartData = value;
}
}
这篇关于维护收藏列表/的ObservableCollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!