代码之家  ›  专栏  ›  技术社区  ›  Usman Zafer

InotifyPropertiesChanged列表视图

  •  1
  • Usman Zafer  · 技术社区  · 6 年前

    我刚接触过Xamarin表单,并尝试在我的应用程序中实现无限循环功能。我所遵循的示例代码运行良好,我已成功地将其集成到我的应用程序中。

    问题是我无法找出如何从代码中更改ListView的源代码。

    我打算在我的ListView上面使用按钮(如下图所示),单击后,ListView的源代码将发生更改。

    这就是我的代码的样子

    <contentpage.bindingContext>
    <local:主视图模型/>
    </contentpage.bindingContext>
    
    <堆叠布局>
    
    <listview x:name=“tyres_listview”itemssource=“绑定项”
    hasUnevenRows=“true”SeparatorVisibility=“none”>
    
    <listview.behaviors>
    <extended:无限滚动行为isloadingmore=“绑定忙”/>
    </listview.behaviors>
    <listview.itemtemplate>
    
    <数据模板>
    <查看单元格>
    
    <堆叠布局>
    <grid margin=“10”VerticalOptions=“FillAndExpand”HorizontalOptions=“FillAndExpand”>
    <grid.columndefinitions>
    <columnDefinition width=“0.25*”></columnDefinition>
    
    <columnDefinition width=“0.75*”></columnDefinition>
    </grid.columndefinitions>
    
    <stacklayout grid.column=“0”horizontaloptions=“fillandexpand”verticalocations=“fillandexpand”margin=“5,5,5,10”>
    <image source=“绑定图像_url”>
    &图像/GT;
    </stacklayout>
    <stacklayout grid.column=“1”spacing=“0”>
    <label text=“绑定名称”fontattributes=“bold”fontsize=“small”margin=“0,5,5,0”VerticalOptions=“Center”></label>
    <label text=“绑定品牌”verticaloptions=“center”fontsize=“micro”></label>
    <label text=“绑定项目_id”verticaloptions=“center”fontsize=“micro”></label>
    
    <stacklayout orientation=“horizontal”x:name=“sl_db”>
    <image source=“fuel.png”></image>
    <label text=“绑定燃料类型”></label>
    <image source=“weather.png”></image>
    <label text=“绑定Wheather类型”></label>
    <image source=“volume.png”></image>
    <label text=“绑定噪音”></label>
    <label text=“db”></label>
    </stacklayout>
    
    
    <stacklayout orientation=“水平”>
    <image></image>
    <label text=“绑定额定值”></label>
    </stacklayout>
    
    <stacklayout orientation=“水平”>
    <label text=“price:”VerticalOptions=“Center”></label>
    <label text=“绑定价格”verticaloptions=“center”textcolor=“green”></label>
    <label text=“eur”VerticalOptions=“Center”></label>
    
    </stacklayout>
    <stacklayout orientation=“水平”>
    <stacklayout.gesturerecognizers>
    <TapGestureRecognizer tapped=“TapGestureRecognizer tapped_1”>
    
    </TapGestureRecognizer>
    </stacklayout.gesturerecognizers>
    <stacklayout backgroundcolor=“red”orientation=“horizontal”margin=“5”>
    <image source=“shoppingcart.png”margin=“10,0,0,0”></image>
    <label text=“Add to Cart”verticaloptions=“center”horizontaloptions=“endExpand”margin=“0,0,10,0”></label>
    </stacklayout>
    <image x:name=“button_info”source=“info.png”>
    
    <image.gesturerecognizers>
    <TapGestureRecognizer tapped=“button ou info ou clicked”></TapGestureRecognizer>
    </image.gesturerecognizers>
    
    &图像/GT;
    </stacklayout>
    
    </stacklayout>
    
    和/格栅& GT;
    </stacklayout>
    
    </viewcell>
    
    </datatemplate>
    
    </listview.itemtemplate>
    
    <listview.footer>
    <grid padding=“6”isvisible=“绑定忙”>
    &!--将页脚设置为不可见时高度为零-->
    <grid.triggers>
    <trigger targettype=“grid”property=“isvisible”value=“false”>
    <setter property=“heightrequest”value=“0”/>
    </trigger>
    </grid.triggers>
    &!--加载内容-->
    <label text=“loading…”textcolor=“deeplink”fontsize=“20”fontattributes=“bold”verticaloptions=“center”horizontaloptions=“center”/>gt;
    和/格栅& GT;
    </listview.footer>
    </listview>
    <button clicked=“button_change_order_clicked”></button>
    </stacklayout>
    < /代码> 
    
    

    public class mainviewModel:InotifyPropertyChanged
    {
    私人旅馆很忙;
    private const int pagesize=10;
    readonly dataservice_dataservice=new dataservice();
    
    公共无限滚动集合<product_search>项get;
    
    公共布尔正忙
    {
    get=>\u正忙;
    设置
    {
    _ IsBusy=值;
    onPropertyChanged();
    }
    }
    公共主视图模型()
    {
    items=新的无限滚动集合<产品搜索>
    {
    onloadmore=async()=>
    {
    Is忙碌=真;
    
    //加载下一页
    var page=items.count/pagesize;
    var items=await-dataservice.getitemsasync(page,pagesize);
    
    Is忙碌=假;
    
    //返回需要添加的项
    退货项目;
    },请
    oncanloadMore=()=>
    {
    返回项目。计数<44;
    }
    };
    
    downloadDataAsync();
    }
    
    私有异步任务下载数据异步()
    {
    var items=await-dataservice.getitemsasync(pageindex:0,pagesize:pagesize);
    items.addrange(项);
    }
    
    公共事件propertychangedeventhandler propertychanged;
    
    受保护的虚拟void OnPropertyChanged([CallerMemberName]String PropertyName=null)
    {
    属性已更改?.invoke(this,new propertychangedeventargs(propertyname));
    }
    }
    < /代码> 
    
    

    }/P>

    任何帮助都是非常重要的。

    问题是我无法找出如何从代码中更改ListView的源代码。

    我打算在我的ListView上面使用按钮(如下图所示),单击后,ListView的源代码将发生更改。

    Image

    这就是我的代码的样子

     <ContentPage.BindingContext>
        <local:MainViewModel />
    </ContentPage.BindingContext>
    
    <StackLayout>
    
        <ListView  x:Name="tyres_listview" ItemsSource="{Binding Items}"
                           HasUnevenRows="True" SeparatorVisibility="None"  >
    
            <ListView.Behaviors>
                <extended:InfiniteScrollBehavior IsLoadingMore="{Binding IsBusy}" />
            </ListView.Behaviors>
            <ListView.ItemTemplate>
    
                <DataTemplate>
                    <ViewCell>
    
                        <StackLayout>
                            <Grid Margin="10" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="0.25*"></ColumnDefinition>
    
                                    <ColumnDefinition Width="0.75*"></ColumnDefinition>
                                </Grid.ColumnDefinitions>
    
                                <StackLayout  Grid.Column="0" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Margin="5,5,5,10">
                                    <Image  Source="{Binding image_url}"  >
                                    </Image>
                                </StackLayout>
                                <StackLayout Grid.Column="1" Spacing="0" >
                                    <Label Text="{Binding name}" FontAttributes="Bold" FontSize="Small" Margin="0,5,5,0" VerticalOptions="Center" ></Label>
                                    <Label  Text="{Binding brand}" VerticalOptions="Center" FontSize="Micro"  ></Label>
                                    <Label  Text="{Binding item_id}" VerticalOptions="Center" FontSize="Micro"  ></Label>
    
                                    <StackLayout Orientation="Horizontal" x:Name="sl_db" >
                                        <Image Source="fuel.png" ></Image>
                                        <Label Text="{Binding fuel_type}"></Label>
                                        <Image Source="weather.png"></Image>
                                        <Label Text="{Binding wheather_type }"></Label>
                                        <Image Source="volume.png"  ></Image>
                                        <Label Text="{Binding noise}"></Label>
                                        <Label Text="dB"></Label>
                                    </StackLayout>
    
    
                                    <StackLayout Orientation="Horizontal">
                                        <Image></Image>
                                        <Label Text="{Binding rated_count }"></Label>
                                    </StackLayout>
    
                                    <StackLayout Orientation="Horizontal">
                                        <Label Text="Price: " VerticalOptions="Center"></Label>
                                        <Label Text="{Binding price }" VerticalOptions="Center" TextColor="Green"></Label>
                                        <Label Text=" EUR" VerticalOptions="Center"></Label>
    
                                    </StackLayout>
                                    <StackLayout Orientation="Horizontal">
                                        <StackLayout.GestureRecognizers>
                                            <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped_1" >
    
                                            </TapGestureRecognizer>
                                        </StackLayout.GestureRecognizers>
                                        <StackLayout BackgroundColor="Red" Orientation="Horizontal" Margin="5" >
                                            <Image Source="shoppingcart.png" Margin="10,0,0,0"></Image>
                                            <Label Text="Add to Cart" VerticalOptions="Center" HorizontalOptions="EndAndExpand" Margin="0,0,10,0" ></Label>
                                        </StackLayout>
                                        <Image x:Name="button_info"  Source="info.png" >
    
                                            <Image.GestureRecognizers>
                                                <TapGestureRecognizer Tapped="button_info_Clicked"></TapGestureRecognizer>
                                            </Image.GestureRecognizers>
    
                                        </Image>
                                    </StackLayout>
    
                                </StackLayout>
    
                            </Grid>
                        </StackLayout>
    
                    </ViewCell>
    
                </DataTemplate>
    
            </ListView.ItemTemplate>
    
            <ListView.Footer>
                <Grid Padding="6" IsVisible="{Binding IsBusy}">
                    <!-- 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="DeepPink" FontSize="20" FontAttributes="Bold" VerticalOptions="Center" HorizontalOptions="Center" />
                </Grid>
            </ListView.Footer>
        </ListView>
        <Button Clicked="button_Change_Order_Clicked"></Button>
    </StackLayout>
    

     public class MainViewModel : INotifyPropertyChanged
    {
        private bool _isBusy;
        private const int PageSize = 10;
        readonly DataService _dataService = new DataService();
    
        public InfiniteScrollCollection<Product_Search> Items { get; }
    
        public bool IsBusy
        {
            get => _isBusy;
            set
            {
                _isBusy = value;
                OnPropertyChanged();
            }
        }
        public MainViewModel()
        {
            Items = new InfiniteScrollCollection<Product_Search>
            {
                OnLoadMore = async () =>
                {
                    IsBusy = true;
    
                    // load the next page
                    var page = Items.Count / PageSize;
                    var items = await _dataService.GetItemsAsync(page, PageSize);
    
                    IsBusy = false;
    
                    // return the items that need to be added
                    return items;
                },
                OnCanLoadMore = () =>
                {
                    return Items.Count < 44;
                }
            };
    
            DownloadDataAsync();
        }
    
        private async Task DownloadDataAsync()
        {
            var items = await _dataService.GetItemsAsync(pageIndex: 0, pageSize: PageSize);
            Items.AddRange(items);
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    

    }

    任何帮助都是非常重要的。

    1 回复  |  直到 6 年前
        1
  •  1
  •   polkduran    6 年前

    你的 ListView.Items 属性已绑定到 MainViewModel.Items 属性A PropertyChanged event 需要触发以指示属性值的更改(与属性本身或其内容相对应,如果 InfiniteScrollCollection<> 是一个可观察的集合)。

    当你想替换 Items 来源你可以给它一个新的值,信号它已经改变,你的 ListView 可以刷新:

    private InfiniteScrollCollection<Product_Search> _items;
    public InfiniteScrollCollection<Product_Search> Items 
    { 
        get { return _items; }
        set
        {
            _items = value;
            OnPropertyChanged();
        }
    }