我有一个绑定到BindableCollection(async ObservableCollection)的简单ListView。我在异步方法中填充BindableCollection。刷新后,该集合包含大约130个唯一项。但是,ListView会重复显示许多项目。检查集合的实际内容,我可以看到它们绝对是独一无二的。内容类似于以下
ViewModel
private readonly BindableCollection<Track> _tracks = new BindableCollection<Track>();
private readonly ITrackFeedService _feedService;
public ICollection<Track> Tracks
{
get { return _tracks; }
}
public async void UpdateTracksAsync()
{
IsBusy = true;
_tracks.Clear();
var tracks = await _feedService.LoadAsync();
_tracks.AddRange(tracks);
IsBusy = false;
}
视图(通过caliburn.micro约定绑定)
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ListView Name="Tracks"
ItemTemplate="{StaticResource TrackDataTemplate}"
Grid.Row="1">
</ListView>
</Grid>
<DataTemplate x:Key="TrackDataTemplate">
<Grid cm:Bind.Model="{Binding}" Margin="0 0 0 5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="10" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Image Name="Artwork" Height="100" Width="100"/>
<Grid Grid.Column="2">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Name="Title" Style="{StaticResource TrackTextStyle}" />
<TextBlock Name="Artist" Grid.Row="1" Style="{StaticResource TrackTextStyle}" />
<TextBlock Name="Album" Grid.Row="2" />
<TextBlock Name="ReleaseYear" Grid.Row="3" />
<TextBlock Name="Label" Grid.Row="4" />
</Grid>
</Grid>
</DataTemplate>
Feed Service
public Task<IEnumerable<Track>> LoadAsync()
{
var scraper = new TrackWebScraper(URL);
return scraper.Scrape();
}
我尝试用标准的ObservableCollection替换BindableCollection,结果相同。我确定我的服务正在返回一系列独特的物品。在抓取新项目之前清除集合时,我可以看到ListView清除。我错过了什么?
答案 0 :(得分:0)
试试这个
_tracks = null;
var tracks = await _feedService.LoadAsync();
_tracks = tracks;