Xamarin标签在Mvvm ListView中初始后未更新

时间:2018-12-18 14:58:26

标签: xamarin mvvm xamarin.forms

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

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>

viewmodel:

单击加号按钮后,将执行以下代码:

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

2 个答案:

答案 0 :(得分:1)

我认为您要传递给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--;
        }
    }

答案 1 :(得分:0)

我刚刚对代码进行了一些更改。

                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
                            product_Value2.Num  = pdt.Num; 


                        }

                    }
                }

现在ui每次都在更新。工作正常...