Xamarin在属性上更改了列表视图

时间:2019-02-06 14:42:13

标签: listview xamarin.forms

我有这个InfiniteScrollCollection列表视图,该列表视图是从Web API获取数据的,并且我有一个onpropertychanged方法,我需要在更改属性时立即使列表视图中的项目更新

       public partial class AddOrderPage : ContentPage, INotifyPropertyChanged
        {
     public InfiniteScrollCollection<Sales_Order_Items> Items { get; }
            public AddOrderPage()
            {
                InitializeComponent();
      Items = new InfiniteScrollCollection<Sales_Order_Items>
                {
                    OnLoadMore = async () =>
                    {
                        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;
                    }
                };
}

这是onpropertychanged方法

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

我不熟悉属性更改,因此应该在哪里以及如何调用该函数以立即更新列表视图?

Sales_Order_Items类:

 public class Sales_Order_Items
    {
        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; }
        public int displayed_quantity { get; set; }
        public float discount { get; set; }
        public double totalprice { get; set; }
        public double totaldiscount { get; set; }
        public double totaltax { get; set; }
        public bool is_new { get; set; }
        public bool has_promotion { get; set; }
        public string last_order_item_date { get; set; }
        public string last_order_item_quantity { get; set; }
        public bool slashvisivble { get; set; }
        public bool discounted { get; set; }
        public bool promotioned { get; set; }
        public Color promotioncolor { get; set; }
        public Color newcolor { get; set; }
        public string usercode { get; set; }
        public bool istotalprice { get; set; }
        public bool isdraft { get; set; }
        public Color lineborderitemcolor { get; set; }
}

我的Xaml类:

 <ListView x:Name="orderslv"  HasUnevenRows="True"  HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" BackgroundColor="White" SeparatorVisibility="None" Grid.Column="0" Grid.Row="2">
                    <ListView.Behaviors>
                        <extended:InfiniteScrollBehavior IsLoadingMore="{Binding isbusy}" />
                    </ListView.Behaviors>
                    <ListView.Header>
                        <StackLayout>
                            <Label TextColor="#FF0000" IsVisible="False" x:Name="error_label" Text="No items in this category" FontFamily="{StaticResource MyriadProRegularFont}" HorizontalTextAlignment="Center">
                                <Label.FontSize>
                                    <OnIdiom Phone="20" Tablet="30"/>
                                </Label.FontSize>
                            </Label>
                        </StackLayout>
                    </ListView.Header>
                    <ListView.Footer>
                        <Grid Padding="6" x:Name="grid">
                            <!-- set the footer to have a zero height when invisible -->
                            <Grid.Triggers>
                                <Trigger TargetType="Grid" Property="IsVisible" Value="False">
                                    <Setter Property="HeightRequest" Value="0" />
                                </Trigger>
                            </Grid.Triggers>
                            <!-- the loading content -->
                            <Label Text="Loading..." TextColor="#039BE5" VerticalOptions="Center" HorizontalOptions="Center" FontFamily="{StaticResource MyriadProSemiBoldFont}">
                                <Label.FontSize>
                                    <OnIdiom Phone="20" Tablet="30"/>
                                </Label.FontSize>
                            </Label>
                        </Grid>
                    </ListView.Footer>
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <customControls:ExtendedViewCell SelectedBackgroundColor="Transparent">
                                <ViewCell.View>


                                                    <controls:CustomStepper Grid.Column="0" Grid.Row="1" HorizontalOptions="CenterAndExpand" OnValueChanged="CustomStepper_OnValueChanged"  Text="{Binding requested_quantity }" >
                                                    </controls:CustomStepper>
                                                    <Frame IsClippedToBounds="True" Padding="0" HasShadow="True" CornerRadius="4" Grid.Column="0" Grid.Row="2">
                                                        <Button Text="ADD TO CART" 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>
                                                </Grid>
                                            </Grid>
                                        </Grid>
                                    </Frame>
                                </ViewCell.View>
                            </customControls:ExtendedViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

当我单击按钮时,我需要立即更新绑定到customstepper文本的“ requested_quantity”属性,我需要您通过该属性的onpropertychanged方法来帮助我,并进行调用以使其更改直接在列表视图上。

预先感谢

1 个答案:

答案 0 :(得分:1)

您需要在要更新的对象上实现INotifyPropertyChanged。在这种情况下,似乎您仅在视图模型(针对整个页面)上实现了它。这也非常有用,但是如果要更新Sales_Order_Items,则需要在其上实现接口和方法。

之后,一切应该开始工作。