我有一个ListView,其中包含一个包含图像和文本的模板。 当我刷新列表时,有时会显示一些图像(随机)
如果我向下滚动列表并向上滚动以再次查看该项目,图像将会神奇地显示,因此图像已正确设置为该项目。
我尝试了ListViewCachingStrategy.RecycleElement和ListViewCachingStrategy.RetainElement,没有区别。
<ListView ItemsSource="{Binding Path=Results}"
HasUnevenRows="True"
IsPullToRefreshEnabled="True"
RefreshCommand="{Binding RefreshCommand}"
IsRefreshing="{Binding IsBusy, Mode=OneWay}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/> <!-- Image -->
<ColumnDefinition Width="3*"/> <!-- Message -->
<ColumnDefinition Width="1*"/> <!-- Date -->
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<!-- Image -->
<Grid Grid.Column="0" Grid.Row="0" Grid.RowSpan="3"
HorizontalOptions="FillAndExpand"
BackgroundColor="#40000000">
<Image Source="{Binding Thumbnail}"
VerticalOptions="Center"
Aspect="AspectFill"/>
</Grid>
private async Task RefreshAsync()
{
try
{
IsBusy = true;
_results.Clear();
_results.AsyncQueryStarted();
var result = await _client.QueryAsync<ReportResult>(_report);
if (result != null)
{
try
{
var items = new List<ResultItem>(result.Events.Count);
foreach (var res in result.Events)
{
result.EntityLookup.TryGetValue(res.Guid, out var name);
var item = new ResultItem(res.Guid, name, res.EventTime, res.Notes);
items.Add(item);
}
int itemWidth = (int)(PageWidth / ItemsPerRow);
// Sort them by timestamp (latest first)
foreach (var item in items.OrderByDescending(x => x.Timestamp))
{
item.ItemWidth = itemWidth;
_results.Add(item);
}
}
catch { }
_results.AsyncQueryFinished(_results.Count == 0);
RefreshView();
}
}
catch (Exception ex)
{
Application.Current.Trace("Error occured while refreshing", ex, this);
}
finally
{
_requiresRefresh = false;
IsBusy = false;
}
}
private async void RefreshView()
{
int itemWidth = (int)(PageWidth / ItemsPerRow);
var requests = new List<ThumbnailItem>();
var results = new List<ResultItem>(_results);
foreach (var item in results)
{
item.ItemWidth = itemWidth;
// Try to retrieve it from cache
item.Thumbnail = await _thumbnailService.RetrieveAsync(item.Id, null, item.Timestamp);
}
}