Repository navigation
Description
Current behavior
In a virtualized ListView whose item template contains a BitmapImage with a remote UriSource, calling ScrollIntoView on an item far down the list right after ItemsSource is set leaves one or two of the images at the bottom of the newly visible region blank. The affected items are the last ones materialized to fill the viewport after the scroll. For a given viewport height it reproduces most of the time but not always. The images never appear afterwards, until the container is recycled for a different item or the list is reloaded.
Reproduces identically on the desktop (Skia) and WebAssembly (Skia) heads, and identically whether the bitmap is shown by an Image element or by an ImageBrush on a Border, with Uno.WinUI 6.8.0-dev.212 / Uno.Sdk 6.8.0-dev.60 on .NET 10.
Template shape (fixed-size tile):
<DataTemplate x:DataType="local:SlideItem">
<StackPanel Padding="0,10">
<Grid Width="90" Height="60">
<Image Stretch="UniformToFill">
<Image.Source><BitmapImage UriSource="{x:Bind ThumbnailUri}" /></Image.Source>
</Image>
</Grid>
<TextBlock Text="{x:Bind Title}" />
</StackPanel>
</DataTemplate>Code-behind (the deferral is the workaround for #24332; the images stay blank with or without it):
private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
var listView = (ListView)sender;
DispatcherQueue.TryEnqueue(() => {
if (listView.SelectedItem is { } selected)
listView.ScrollIntoView(selected);
});
}Steps:
- Set
ItemsSourceto ~100 items with distinct remote thumbnail URLs. - Set
SelectedItemto an item near the end (e.g. index 58) and callScrollIntoViewfor it. - Observe the list after the scroll: the last one or two tiles inside the viewport have no image. Resizing the window changes which tiles are affected.
Expected behavior
Every BitmapImage whose download completes successfully renders, regardless of what other BitmapImage instances for the same URL do.
Cause
BitmapImage.TryOpenSourceAsync (src/Uno.UI/UI/Xaml/Media/Imaging/BitmapImage.cs) keys an in-flight Task<ImageData> in _bitmapImageCache by (URI, decode size) and hands that same task to every BitmapImage that opens the same URL while it is running. The task is created with the cancellation token of the first requester (Task.Run(..., ct), and GetImageDataFromUriAsCompositionSurface(uri, ct) threads it through the stream fetch and the read), and the token's registration removes the cache entry when it fires.
ImageSource.Open() (ImageSource.crossruntime.cs) cancels the previous open whenever it is called again on the same instance (_opening.Disposable = Disposable.Create(cts.Cancel)), which happens on every UriSource change through InvalidateSource().
With the managed virtualizing panel this combination fails as follows:
- A container is realized for item N and its
BitmapImagestarts the download; the task is cached. - The panel recycles that container (
RecycleViewForItem). Pooled containers keep theirDataContext, so the bitmap keeps downloading. - Item N is realized again, through a different container popped from the pool (
TryDequeueCachedContaineris LIFO, so it is often not the same one). ItsBitmapImagehits the cache and awaits the first container's task. - The first container is reused for item M:
UriSourcechanges,InvalidateSource()→Open()cancels the first token. Inside the shared task,GetImageDataFromUriAsCompositionSurfacecatches theOperationCanceledExceptionand returnsImageData.FromError(...), so the task completes with an error rather than cancelling. - The second container receives that error:
RaiseImageFailed,OnOpened(error), blank tile. The cache entry has been removed by the registration, so a later subscriber would download fresh, but nothing retries.
Steps 1 to 3 are precisely what ScrollIntoView does to the items at the viewport edge (materialize to fill, correct the estimate, unfill, refill), which is why the last visible tiles are the ones affected, and whether the download finishes before step 4 is what makes it intermittent.
Workaround
CreateOptions="IgnoreImageCache" on the BitmapImage in the template, so each container downloads independently.
Suggested fix
Either run the cached download on a token that is independent of any single requester (for example cancel it only when the last subscriber goes away, or never, since the entry is evicted by the LRU anyway), or when a requester's own token fires, detach it from the shared task instead of cancelling the task for everyone.
Environment
- Uno.WinUI 6.8.0-dev.212, Uno.Sdk 6.8.0-dev.60, Uno.Wasm.Bootstrap 10.1.0-dev.219
- .NET 10.0.401 SDK, runtime pack 10.0.12
- Heads: net10.0-desktop (Windows 11, Skia) and net10.0-browserwasm (Skia renderer, Chromium-based browsers)
Activity
fix(imaging): Keep a shared BitmapImage download alive when one reque…
fix(imaging): Drop a failed shared BitmapImage load from the cache
fix(imaging): Keep a shared BitmapImage download alive when one reque…
fix(imaging): Drop a failed shared BitmapImage load from the cache
fix(imaging): Keep a shared BitmapImage download alive when one reque…
fix(imaging): Drop a failed shared BitmapImage load from the cache