Skip to content

[Validation] Analyzer diagnostics for Virtualize item identity #68485

Description

@oroztocil

Scenario contact: @ilonatommy

Scenario

A virtualized list that loads items on demand can make the viewport jump when the underlying data changes, and the cause is hard to trace: the component cannot tell one item from another, so it cannot work out whether items arrived before or after what the user is looking at. This validates that the build now warns about it, and that following the advice actually fixes the behavior.

Minimum build

.NET 11 Preview 7 or later.

Configurations to cover

  • Blazor Web App
    • Static SSR
    • Interactive Server
    • Interactive WebAssembly
    • Interactive Auto
  • Standalone WebAssembly
  • Hybrid (MAUI)

These diagnostics are produced by the compiler, so they do not vary by render mode. The configuration below is the one needed to reproduce the runtime behavior the warning predicts, not a matrix to work through.

Also exercise

  • Published output
  • An existing .NET 10 app upgraded to .NET 11
  • Trimming or ahead-of-time compilation
  • More than one server instance, or a proxy in front
  • Hot Reload
  • An IDE as well as the command line
  • Container

Setup

The diagnostic this scenario covers is:

Id Title Code fix
BL0011 Virtualize with ItemsProvider requires ItemComparer No

What to build

A page with a long virtualized list whose items come from an ItemsProvider, so that you can insert items at the top and at the bottom while the user is scrolled into the middle of the list. Write it first without setting ItemComparer, and make the item type a class rather than a record, since the default comparer falls back to reference equality for classes and that is what the warning is about.

Add controls to insert items above and below the visible range, and show each item's index so you can tell where the viewport ended up.

Things to try

  • Building at the command line and in an IDE, and reading the messages that appear.
  • Scrolling into the middle of the list and then adding items above and below the visible range.
  • Setting ItemComparer, then repeating the scrolling test.
  • A Virtualize that supplies Items directly rather than through ItemsProvider, to see whether it is flagged.
  • An item type that is a record rather than a class, to see whether that changes the warning.
  • The same list rendered through QuickGrid rather than Virtualize.

Expected behavior

The problem is reported at build time, and setting ItemComparer fixes the runtime behavior the warning predicted.

Must hold

  • The list written without ItemComparer produces a BL0011 warning at the correct line.
  • Before ItemComparer is set, inserting items above the visible range moves the scroll position.
  • After ItemComparer is set, the list keeps its scroll position when items are inserted above the visible range.
  • A Virtualize that supplies Items directly, rather than through ItemsProvider, produces no BL0011 warning.
  • The warning appears both at the command line and in the IDE, at the same line.

Expected differences between configurations

  • None. The warning is produced at compile time and is identical in every configuration.

Documentation to use

What to report

Report results using the format described in the validation testing manual. Include link to a repository with the test app.

Metadata

Metadata

Labels

ValidationThis issue is used to track validation effortsarea-blazorIncludes: Blazor, Razor Componentsvalidation-scenarioThis issue describes a validation testing scenario

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions