Forum Xamarin Xamarin.Forms

Listview items are not changing when using INotifyPropertyChanged

mhmdmhmd Member ✭✭

I'm creating an app using Xamarin Forms, in the app I have a layout with a listview that displays items, every item has a stepper and "add to cart" button to order this item. The text of the stepper is binded to a property called displayed_quantity, when I add the item to the cart which is saving in a static list, in the cart, there is an option to remove the item added. What I need is, when I remove the Item from the cart the property diplayed_quantity of this item to be 0 again by using INotifyPropertyChanged with OnAppearing method of this layout.

I will show you my work but unfortunately, it doesn't work:

    public class Sales_Order_Items : BaseViewModel
    {
        public int Id { get; set; }
        public string Item_Code { get; set; }
        public string description { get; set; }
        public string Price_List_Code { get; set; }
        public string CURRENCY_CODE { get; set; }
        public double price { get; set; }
        public string Uom_Code { get; set; }
        public string Client_Code { get; set; }
        public double return_price { get; set; }
        public string Tax_Code { get; set; }
        public double Tax { get; set; }
        public string CURRENCY_SYMBOL { get; set; }
        public double Default_Discount { get; set; }
        public int item_order { get; set; }
        public int distribution_type { get; set; }
        public string Client_Name { get; set; }
        public string family { get; set; }
        public double converted_price { get; set; }
        public double converted_return_price { get; set; }
        public double converted_tax { get; set; }
        public double converted_discount { get; set; }
        public string converted_currency_code { get; set; }
        public string converted_currency_symbol { get; set; }
        public double stock_quantity { get; set; }
        public string expiry_date { get; set; }
        public string path { get; set; }
        public int requested_quantity { get; set; }
        private int _displayed_quantity;
        public int displayed_quantity
        {
            get
            {
                return _displayed_quantity;
            }
            set
            {
                SetProperty(ref _displayed_quantity, value);
            }
        }
}

BaseViewModel Class:

        public class BaseViewModel : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;

            protected virtual void OnPropertyChanged([CallerMemberName]string propertyName = null)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs((propertyName)));
            }

            protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName]string propertyName = null)
            {
                if (EqualityComparer<T>.Default.Equals(storage, value))
                {
                    return false;
                }
                storage = value;
                OnPropertyChanged(propertyName);

                return true;
            }
        }

The contentPage displaying the list:

                                     public partial class AddOrderPage : ContentPage,INotifyPropertyChanged
                                    {
                                        int reqest_quantity = 0;
                                        int quantity = 0;
                                        int index = 0;
                                        int count = 0;
                                        int value = 0;
                                        bool isbusy;
                                        string selectedcategorycode;
                                        string lang = CultureInfo.CurrentCulture.TwoLetterISOLanguageName.ToString();
                                        ObservableCollection<Categories> mylist = new ObservableCollection<Categories>();
                                        ObservableCollection<Sales_Order_Items> filteredlist = new ObservableCollection<Sales_Order_Items>();
                                        ObservableCollection<Sales_Order_Items> newfilteredlist = new ObservableCollection<Sales_Order_Items>();
                                        ObservableCollection<Sales_Order_Items> orderlist = new ObservableCollection<Sales_Order_Items>();
                                        ObservableCollection<Sales_Order_Items> requestedqtylist = new ObservableCollection<Sales_Order_Items>();
                                        private bool _isBusy;
                                        private const int PageSize = 5;
                                        public InfiniteScrollCollection<Sales_Order_Items> Items { get; }
                                        public AddOrderPage()
                                        {
                                            InitializeComponent();
                                            Loading.Text = LanguageResource.loading;
                                            error_label.Text = LanguageResource.noitemsinthiscategory;
                                            if (TestingClass.Usercartlist.Count > 0)
                                                BadgeTextLabel.Text = TestingClass.Usercartlist.Count.ToString();
                                            if (TestingClass.Usercartlist.Count <= 0)
                                            {
                                                BadgeTextLabel.Text = "";
                                            }
                                            GetCategories();
                                            DownloadDataAsync();
                                            Items = new InfiniteScrollCollection<Sales_Order_Items>
                                            {
                                                OnLoadMore = async () =>
                                                {
                                                    OnPropertyChanged();
                                                    IsBusy = true;
                                                    grid.IsVisible = true;
                                                    // load the next page
                                                    //_dataService.setitemsDB();
                                                    var page = Items.Count / PageSize;
                                                    // var items = await GetItemsAsync(page, PageSize);
                                                    var items = await GetItemsAsync(selectedcategorycode, ++index, searching.Text);
                                                    //Console.WriteLine(items);
                                                    IsBusy = false;
                                                    grid.IsVisible = false;
                                                    // return the items that need to be added
                                                    return items;
                                                },
                                                OnCanLoadMore = () =>
                                                {
                                                    return newfilteredlist.Count > 0;
                                                }
                                            };
                                            TestingClass.entered = 1;
                                        }


                                    public event PropertyChangedEventHandler PropertyChanged;
                                            protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
                                            {
                                                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
                                            }
                                      private void Addtocart_Clicked(object sender, EventArgs e)
                                        {
                                            var btn = sender as Button;
                                            Sales_Order_Items item = btn.BindingContext as Sales_Order_Items;
                                            if (item != null)
                                            {
                                                if (item.requested_quantity == 0)
                                                {
                                                    item.quantitycolor = Color.FromHex("#039BE5");
                                                    item.requested_quantity = 1;
                                                    item.displayed_quantity = 1;
                                                    order(item);
                                                }
                                                else
                                                if (item.requested_quantity != 0)
                                                    order(item);
                                            }
                                        }

And finally the "add to cart" button in the Xaml file of the content page:

                        <controls:CustomStepper Grid.Column="0" Grid.Row="1" HorizontalOptions="CenterAndExpand" OnValueChanged="CustomStepper_OnValueChanged"  Text="{Binding displayed_quantity}" >
                                                                                </controls:CustomStepper>
                                                                                <Frame IsClippedToBounds="True" Padding="0" HasShadow="True" CornerRadius="4" Grid.Column="0" Grid.Row="2">
                                                                                    <Button Text="{x:Static controls2:LanguageResource.addtocart}" TextColor="#039BE5" BackgroundColor="White"  Clicked="Addtocart_Clicked" Padding="5" >
                                                                                        <Button.FontSize>
                                                                                            <OnIdiom Phone="{OnPlatform Android=12,iOS=8}" Tablet="{OnPlatform Android=16,iOS=14}"/>
                                                                                        </Button.FontSize>
                                                                                        <Button.HeightRequest>
                                                                                            <OnIdiom Phone="34" Tablet="40"/>
                                                                                        </Button.HeightRequest>
                                                                                    </Button>
                                                                                </Frame>

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    Could you please post a basic demo so that we can test with it?

Sign In or Register to comment.