Fix for ItemSpacing value being retained when re-entering CollectionView with ItemsLayout set to vertical or horizontal list after navigating back.
Root cause
The issue was caused because LinearItemsLayout.Vertical and LinearItemsLayout.Horizontal were used as static instances instead of creating new instances in the layout type converter. As these static instances were reused, properties like ItemSpacing retained their previous values when returning to the page using PopAsync. Since the internal items layout property defaulted to the static LinearItemsLayout.Vertical, the layout did not refresh properly, leading to inconsistent or unexpected behavior across all platforms.
Description of Issue Fix
The fix involves replacing the use of static instances (LinearItemsLayout.Vertical and LinearItemsLayout.Horizontal) with newly created instances of LinearItemsLayout to prevent shared state across navigations. This was achieved by updating the layout initialization to use a defaultValueCreator that returns a new LinearItemsLayout instance, and modifying the ItemsLayoutTypeConverter to return new instances based on the orientation string, rather than reusing static ones. This ensures each layout has its own settings, so ItemSpacing doesn't carry over when the page is popped with the back button and opened again using GoToAsync.
Tested the behavior in the following platforms.
- [x] Windows
- [x] Mac
- [x] iOS
- [x] Android
Issues Fixed
Fixes https://github.com/dotnet/maui/issues/28023
Output
| Before Issue Fix | After Issue Fix |
|---|---|
Hey there @@BagavathiPerumal! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.
/azp run MAUI-UITests-public
Azure Pipelines successfully started running 1 pipeline(s).
/azp run MAUI-UITests-public
Azure Pipelines successfully started running 1 pipeline(s).
/azp run MAUI-UITests-public
Azure Pipelines successfully started running 1 pipeline(s).
https://github.com/dotnet/maui/pull/29683