【发布时间】:2023-02-20 19:44:57
【问题描述】:
当从列表中的一项更改一个值时,我正在尝试更新页面。
我的 Xaml 中有以下列表视图
<ListView
x:Name="ItemsListView"
CachingStrategy="RecycleElement"
HasUnevenRows="true"
IsPullToRefreshEnabled="true"
IsRefreshing="{Binding IsBusy, Mode=OneWay}"
ItemTapped="ItemsListView_ItemTapped"
ItemsSource="{Binding Items}"
RefreshCommand="{Binding LoadItemsCommand}"
VerticalOptions="FillAndExpand">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout
Padding="10"
HorizontalOptions="FillAndExpand"
Orientation="Horizontal">
<Label
FontSize="16"
HorizontalOptions="FillAndExpand"
LineBreakMode="WordWrap"
Text="{Binding displayProduct}" />
<Label
FontSize="16"
HorizontalOptions="FillAndExpand"
LineBreakMode="WordWrap"
Text="{Binding quantity}" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
在页面代码中,我有一个函数可以搜索 ListItems 之一是否具有相应的 EAN 作为属性,如果是,则增加它的数量。
ListItemScanList itemSelected = new ListItemScanList();
foreach (ListItemScanList item in viewModel.Items)
{
if (item.EAN == barcode)
{
found = true;
itemSelected = item;
break;
}
}
if (found)
{
itemSelected.quantity += 1;
await Common.UpdateScanList(barcode, itemSelected.quantity);
}
在视图模型中,我有通过调用 onpropertychanged 更新的项目列表,但是当我更改值时视图不会更新。难道我做错了什么?
public class ScanListViewModel : BaseViewModel
{
public string _mode = "MULTI";
public ScanList _scanList;
public int _total = 0;
public ScanListViewModel()
{
Title = "Scanning";
Items = new ObservableCollection<ListItemScanList>();
LoadItemsCommand = new Command(async () => await ExecuteLoadItemsCommand());
}
ObservableCollection<ListItemScanList> _items;
public ObservableCollection<ListItemScanList> Items
{
get { return _items; }
set { _items = value; OnPropertyChanged("Items"); }
}
}
【问题讨论】:
-
请给出
ListItemScanList的定义。该类的属性似乎没有引发PropertyChanged事件。您的ObservableCollection<ListItemScanList>仅将更改传播到集合本身,但如果您需要对单个项目的更改做出反应,则模型ListItemScanList也必须发出通知。
标签: c# android xamarin.forms mvvm