【问题标题】:UpdateList objects on property changed属性上的 UpdateList 对象已更改
【发布时间】: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&lt;ListItemScanList&gt; 仅将更改传播到集合本身,但如果您需要对单个项目的更改做出反应,则模型 ListItemScanList 也必须发出通知。

标签: c# android xamarin.forms mvvm


【解决方案1】:

根据您的相关代码,不清楚您是否在更新时为 quantity 属性引发 PropertyChanged 事件。

内部模型中的属性独立通知它们的更改。您需要在 ListItemScanList 类中实现 INotifyPropertyChanged 接口,并在 quantity 属性的设置器中引发 PropertyChanged 事件。这将通知视图 quantity 已更改,它需要更新 UI。

public class ListItemScanList : BaseViewModel
{
    private int _quantity;
    public int quantity
    {
        get { return _quantity; }
        set
        {
            _quantity = value;
            OnPropertyChanged("quantity");
        }
    }
}

【讨论】:

    猜你喜欢
    • 2012-06-15
    • 2018-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多