Xamarin Forms ListView未从ObservableCollection更新

z3yyvxxp  于 9个月前  发布在  其他
关注(0)|答案(4)|浏览(119)

我的XAML文件的ListView正在填充一个ViewModel,该ViewModel具有来自服务的ObservableCollection,但ListView没有显示信息。我已经检查了服务是否返回正确的信息。
这是我的XML文件的代码:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="XAMLUnit1.Views.NetflixRoulettePage" Title="Movie">
    <ContentPage.Content>
         <StackLayout>
         <AbsoluteLayout>
                <BoxView Color="Gray"  AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0,0,1,1"></BoxView>
                <SearchBar x:Name="searchBar"
                           Placeholder="Search By Actor's name" 
                           PlaceholderColor="White"
                           AbsoluteLayout.LayoutFlags="All"
                           AbsoluteLayout.LayoutBounds="0.1,0.1,1,1"  SearchCommand="{Binding SearchMovieCommand}" ></SearchBar>
            </AbsoluteLayout>

       <ListView  x:Name="ListOfMovies"  
            ItemsSource="{ Binding MovieList}" >
            <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout>
                                <ImageCell 
                                        ImageSource="{Binding poster_path, StringFormat='https://image.tmdb.org/t/p/w500{0}'}">
                                </ImageCell>
                                    <StackLayout Orientation="Horizontal">
                                    <TextCell Detail="{Binding title}"></TextCell>
                                    <TextCell Detail="{Binding release_date}"></TextCell>
                                </StackLayout>
                            </StackLayout>
                        </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

字符串
这是调用服务的ViewModel,它使用其ObservableCollection作为ListView的ItemsSource:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms;
using XAMLUnit1.Models;
using XAMLUnit1.ServiceImpl;
using XAMLUnit1.Services;

namespace XAMLUnit1.ViewModels
{
  public  class MovieRouletteViewModel
    {
        IMovieService service;
        public ObservableCollection<Movie> MovieList { get; set; }
        public ICommand SearchMovieCommand { get; set; }
        public MovieRouletteViewModel()
        {
            service = new MovieServiceFinder();
            SearchMovieCommand = new Command(GetMovies);
        }

        private void  GetMovies()
        { var list= service.GetMovies("");
            MovieList = list;

        }

    }
}



public partial class NetflixRoulettePage : ContentPage
    {
        MovieRouletteViewModel viewModel;
        public NetflixRoulettePage()
        {
            InitializeComponent();
            viewModel = new MovieRouletteViewModel();
            BindingContext = viewModel;

        }


        private void SearchBar_TextChanged(object sender, TextChangedEventArgs e)
        {
        }
    }

rqcrx0a6

rqcrx0a61#

不要将ObservableCollection设置为一个新的List,它会破坏绑定。从列表中清除项目并向其中添加新项目。

public MovieRouletteViewModel()
    {
        service = new MovieServiceFinder();
        SearchMovieCommand = new Command(GetMovies);
        MovieList = new ObservableCollection<Movie>();
    }

    private void  GetMovies()
    { 
        var list= service.GetMovies("");
        MovieList.Clear();
        foreach(Movie movie in list)
        {
              MovieList.Add(movie);
        }
    }

字符串

qhhrdooz

qhhrdooz2#

有几件事我想注意这个问题,虽然主要是作为评论正确地说明,更换一个新的集合打破了绑定的集合。因此,为什么它没有更新。
但是,有几种解决方案需要考虑。
1.在集合上实现INotifyPropertyChanged,然后像你现在做的那样替换整个集合。这将很好地工作。但是它确实重置了滚动,并且基本上从头开始重新计算所有内容。有些人会说这打败了ObservableCollection的意义,因为它们有自己的内置通知管道。是的,确实如此。尽管如此,如果你要用一个新的集合替换整个集合,那么与clearRing和单独添加每个项目相比,你将保存大量的循环计算,这基本上会在每次更新时触发
1.对每个项目分别调用ClearAdd。如果集合没有太大变化,你甚至可以更进一步,只比较两个集合并更新所需的内容。但是,如果集合不相似,那么这种方法仍然是昂贵的,并且在移动终端上,你希望尽可能减少屏幕重新计算。
1.创建一个子类化的集合,实现您自己的replace/update和Add/Remove范围方法,INotifyPropertyChanged为您提供了两全其美的功能,这将允许对集合进行原子修改,然后您可以触发一次属性更改事件。
网上有很多这样的例子,值得一提的是,有时候在移动的设备上清理和添加物品并不是最好的方法。这取决于你的收藏有多大,变化有多大,变化的原因是什么。

  • 如果是一个完全不同的名单,那么更换收藏在我看来是没问题的。
  • 如果它是相同的列表,那么您可能想要比较和修改以保存属性更改

不管怎样祝你好运

piv4azn7

piv4azn73#

我不同意其他的答案。你可以设置Movies很多次,你想要的,这不是问题。
问题是您的viewmodel没有实现INotifyPropertyChanged
当你设置你的ObservableCollection时,你的UI不会被通知。

zc0qhyus

zc0qhyus4#

您的service命令实际上正在替换ObservableCollection,您需要将GetMovies()方法更改为

var list = service.GetMovies("");
MovieList.clear();
foreach(Movie m in list)
{ MovieList.Add(m);}

字符串

相关问题