xamarin - Xamarin标签在Mvvm listview中初始后未更新

标签 xamarin mvvm xamarin.forms

我的页面中有2个列表 View 。第一个列表水平显示类别。单击类别后,第二个列表在下面显示了相应的产品。第二个列表的每个列表项都包含用于将产品添加到购物车的加号和减号按钮。单击按钮可更新标签上每个产品的数量。标签从其他类别移出后未在ui中更新。按钮单击有效,在调试时,标签值已在viewmodel中正确更新,但自第二次以来未在ui中反射(reflect)。需要帮助。

xaml:

<ListView  x:Name="pdt_list" HasUnevenRows="True" SeparatorVisibility="None" ItemsSource="{Binding Productlist}" BackgroundColor="White" Margin="0,10,0,0" >
            <ListView.ItemTemplate>
                <DataTemplate >
                    <ViewCell >
                        <ViewCell.View>
                             <Frame HasShadow="False" Margin=" 0,10,0,10"  Padding="10,5,10,5" BackgroundColor="#e9e9e9" HeightRequest="80" HorizontalOptions="FillAndExpand" VerticalOptions="CenterAndExpand" >
                                  <Grid>
                                            <!--<StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="CenterAndExpand"  Margin="0,10,0,10"  >-->

                                  <StackLayout VerticalOptions="FillAndExpand" Margin="0" Padding="10,0,0,0" Orientation="Horizontal" Opacity="{Binding opacity}">

                                     <Image Source="{Binding image}"  Aspect="AspectFill"  WidthRequest="70" HeightRequest="180"  VerticalOptions="FillAndExpand" />

                                      <StackLayout HorizontalOptions="FillAndExpand" Orientation="Vertical"  >

                                        <Label Text="{Binding product_name}" Font="Bold" VerticalTextAlignment="Center" FontSize="Medium" TextColor="Black" FontFamily="opensans_light.ttf#opensans_light" Margin="10,20,0,0" />
                                            <StackLayout Orientation="Horizontal" Margin="10,0,0,0" HorizontalOptions="Start" VerticalOptions="Start"  >
                                                        <Label Text="{Binding rupee}"  TextColor="#FA2E27" HeightRequest="90" 
                                                            FontSize="Medium" HorizontalOptions="Start" VerticalTextAlignment="Start" VerticalOptions="FillAndExpand" />
                                                        <Label Text="{Binding selling_price}" Font="Bold"  HorizontalOptions="Start" Margin="0" TextColor="#FA2E27" VerticalTextAlignment="Start" FontSize="Medium" FontFamily="opensans_light.ttf#opensans_light" />

                                           </StackLayout>
                                      </StackLayout>
                                      <ImageButton Source="carts.png" BackgroundColor="Transparent" IsVisible="False" HorizontalOptions="EndAndExpand" WidthRequest="40" HeightRequest="40" VerticalOptions="CenterAndExpand"  Clicked="Add_cart_Clicked" Margin="0,20,0,0" Aspect="AspectFit" />


                                    <StackLayout Orientation="Horizontal" BackgroundColor="Transparent"   HorizontalOptions="EndAndExpand">
                                        <ImageButton Source="minus.png" HorizontalOptions="EndAndExpand"   BackgroundColor="Transparent"  WidthRequest="25" HeightRequest="25" Clicked="Minus_Tapped" />
  //this label is not updating          <Label Text="{Binding Num}" VerticalTextAlignment="Center" HorizontalOptions="EndAndExpand"   FontSize="Medium" TextColor="Black" FontFamily="opensans_light.ttf#opensans_light" Margin="0,0,0,0" /> 
                                        <ImageButton Source="plus2.png" HorizontalOptions="EndAndExpand"   BackgroundColor="Transparent"   WidthRequest="25" HeightRequest="25"  Clicked="Plus_Tapped"   />
                                    </StackLayout>
                                            </StackLayout>

                                            <StackLayout BackgroundColor="Black" HorizontalOptions="EndAndExpand" VerticalOptions="StartAndExpand" WidthRequest="100" HeightRequest="25" IsVisible="{Binding opaque}" Margin="0,0,0,0" >

                                                <Label Text="Not Available" FontFamily="opensans_light.ttf#opensans_light" TextColor="White" FontAttributes="Bold" HorizontalOptions="Center" VerticalTextAlignment="Center" />
                                            </StackLayout>
                                  </Grid>
                             </Frame>
                        </ViewCell.View>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

View 模型:

单击加号按钮后,将执行以下代码:
public ObservableCollection<Product_Value2> purchaselist = new ObservableCollection<Product_Value2>();

public void Plus_item(Product_Value2 product_Value2)
    {
        var list = new ObservableCollection<Product_Value2>(purchaselist);


        string id = product_Value2.id;
        ProductsRegister reg = new ProductsRegister();

        reg.Name_Product = product_Value2.product_name;
        App.Database.SaveProducts(reg);



        if ((purchaselist.ToList() != null) && (purchaselist.ToList().Any()))
        {
            //  bool alreadyExists = purchaselist.Any(x => x.id.Equals(id));
            if (purchaselist.Any(x => x.id.Equals(id)))
            {
                foreach (Product_Value2 pdt in list)
                {
                    if (pdt.id.Equals(id))
                    {
                        pdt.Num++; // here value is updating everytime,but first time only in ui
                        OnPropertyChanged("Num");
                        break;
                    }

                }
            }



        }



        DependencyService.Get<IToast>().ShortAlert("Added To Cart");

        Number++;
        OnPropertyChanged("Number");

    }

模型:
public class Product_Value2 : INotifyPropertyChanged
    {         
        public string id { get; set; }
        public string image { get; set; }
        public string imagepath { get; set; }
        public string product_name { get; set; }
        public string rupee { get; set; }
        public float selling_price { get; set; }
        public float price { get; set; }
        public string available_qty { get; set; }
        public string count { get; set; }        
        public bool minus_enb { get; set; }
        public bool plus_enb { get; set; }


        bool Visibility;
        public bool visibility
        {
            set
            {
                Visibility = value;
                OnPropertyChanged("visibility");
            }
            get
            {
                return Visibility;
            }
        }

        long num1 ;
        public long Num  //this is the label text
        {
            set
            {
                num1 = value;
                OnPropertyChanged("Num");
            }
            get
            {
                return num1;
            }
        }


        bool Visible;
        public bool visible
        {
            set
            {
                Visible = value;
                OnPropertyChanged("visible");
            }
            get
            {
                return Visible;
            }
        }




        void OnPropertyChanged(string IsVisible)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(IsVisible));
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }

附上下面的屏幕快照显示初始:

first time

下面显示了第二次:

second time

最佳答案

我认为您要传递给Plus_item(Product_Value2 product_Value2)方法的参数是您要添加或减去的项目。因此,您可以简单地更新传入参数本身中的Num。您可以从列表中查找并根据ID在列表中进行更新。下面的示例将使您更加清楚,

async void Plus_Tapped(object sender, System.EventArgs e)
    {
        Product_Value2 product_Value2 = ((Product_Value2)((ImageButton)sender).BindingContext);
        if (product_Value2 != null)
        {
            product_Value2.Num++;
        }
    }

    async void Minus_Tapped(object sender, System.EventArgs e)
    {
        Product_Value2 product_Value2 = ((Product_Value2)((ImageButton)sender).BindingContext);
        if (product_Value2 != null)
        {
            product_Value2.Num--;
        }
    }

关于xamarin - Xamarin标签在Mvvm listview中初始后未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53835751/

相关文章:

c# - 我应该在设置之前检查 VisualElement.IsVisible 吗?

c# - 如何动态地在 xamarin 表单中的标签上创建点击事件

xaml - 如何在 BindableLayout.ItemsSource 中绑定(bind)项目的索引

c# - 如何组织我的 Xamarin.Forms 解决方案?

javascript - 使用来自 JS xamarin 的参数调用 C# 方法

xamarin - 如何使用 Xamarin Forms 更改导航页面标题的字体?

wpf - 具有可编辑集合的 MVVM 示例,其中 View 不直接使用模型类型

wpf - DoDragDrop之后触摸事件不起作用

c# - 在 Xcode/Xamarin 中创建状态栏

c# - 数据绑定(bind)到 WPF TreeView 中的 SelectedItem