我的 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)
{
}
}
不要将 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);
}
}
关于这个问题,我想注意一些事情,尽管主要是因为注释正确地阐明了,用新集合替换集合会破坏绑定。因此为什么它不更新。
但是,有几种解决方案需要考虑。
在集合上实现
INotifyPropertyChanged
并像您正在做的那样替换整个集合,这会很好地工作。然而,它确实重置了滚动并基本上从头开始重新计算所有内容。有些人会说这违背了 ObservableCollection
的意义,因为他们有自己的内置通知管道,是的,确实如此。不过,如果您用新集合替换整个集合,那么与 cleanRing 和单独添加每个项目相比,您将节省大量的循环计算,这基本上会在每次更新时触发分别对每个项目调用
Clear
和 Add
。如果集合没有太大变化,您甚至可以通过比较 2 个集合并更新所需内容来更进一步。然而,如果集合不相似,那么这种方法仍然很昂贵,并且在移动设备上,您希望尽可能减少屏幕重新计算。 创建子类集合,实现您自己的替换/更新和添加/删除范围方法,并且
INotifyPropertyChanged
为您提供两全其美的功能,这将允许对集合进行原子修改,然后您可以触发一次属性更改事件。 网上有很多所有这些方法的示例,只是值得注意的是,清除和添加项目有时并不是移动设备的最佳方法。这取决于您的收藏有多大、变化程度以及原因。
无论如何祝你好运
我不同意其他答案。您可以根据需要多次设置
Movies
,这不是问题。
问题只是你的
viewmodel
没有实现INotifyPropertyChanged
。
当您设置
ObservableCollection
时,您的 UI 不会收到通知。
您的服务命令实际上正在替换
ObservableCollection
,您需要将 GetMovies()
方法更改为
var list = service.GetMovies("");
MovieList.clear();
foreach(Movie m in list)
{ MovieList.Add(m);}
赫洛 我遇到了同样的问题,我尝试清除列表并添加每个项目,但是当填充列表完成时出现以下错误。
'无法访问已处置的对象。 对象名称:“Microsoft.Maui.Controls.Handlers.Compatibility.FrameRenderer”。 有人解决这个问题了吗