dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
21.89k stars 1.69k forks source link

ListView view DataTemplateSelector mixes templates on Android with CachingStrategy on RecycleElement #21780

Open metaltunder opened 3 months ago

metaltunder commented 3 months ago

Description

DataTemplateSelector doesn't work properly on Android when the ListView view has the CachingStrategy option set to RecycleElement. We noticed this issue in our own project a while back, when we started the migration to .NET MAUI from Xamarin Forms. At first, we thought it would have been related to some open issues (e.g. #20842 ), but we were wrong.

The bug disappears as long as we don't use the CachingStrategy option, but it is needed in our project for performance.

It does work properly on iOS and Windows.

Steps to Reproduce

The repo contains a page with 3 buttons that load 3 different collections of different objects into a list using MVVM.

Initially, the data that is loaded is correctly shown in the list. But, changing the data using the other buttons shows how the datatemplateselector messes it up and assigns incorrect templates for the objects given in the list, aswell as not showing them all.

DataTemplateSelector RecycleElement bug.webm

Link to public reproduction project repository

https://github.com/metaltunder/RecycleElementBug

Version with bug

8.0.7 SR2

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 14

Did you find any workaround?

No response

Relevant log output

No response

XamlTest commented 3 months ago

Verified on VS 17.10.0 Preview 3.0(8.0.14). Repro on Android 14.0-API34, not repro on Windows 11, iOS 17.2 and MacCatalyst with below Project: RecycleElementBug.zip