The issue occurred when toggling the header template in a CollectionView. Specifically, when the header was set to null and then re-assigned, an ObjectDisposedException was thrown. This happened because the platform UIView associated with the header was disposed when toggled off, but the handler for the VisualElementwas not cleared, leading to a stale reference.
Description of Change
To resolve the issue, I utilized the DisconnectHandler() method provided by MAUI, which cleanly disconnects the handler from its associated platform view. This ensures that all resources tied to the handler are properly released, preventing any stale references. After calling DisconnectHandler(), formsElement is set to nullto fully clear the reference, allowing the framework to initialize a fresh handler as needed. This approach maintains a consistent view lifecycle and prevents exceptions caused by attempting to access disposed views.
Root Cause
The issue occurred when toggling the header template in a
CollectionView
. Specifically, when the header was set tonull
and then re-assigned, anObjectDisposedException
was thrown. This happened because the platform UIView associated with the header was disposed when toggled off, but the handler for theVisualElement
was not cleared, leading to a stale reference.Description of Change
To resolve the issue, I utilized the
DisconnectHandler()
method provided by MAUI, which cleanly disconnects the handler from its associated platform view. This ensures that all resources tied to the handler are properly released, preventing any stale references. After callingDisconnectHandler()
,formsElement
is set tonull
to fully clear the reference, allowing the framework to initialize a fresh handler as needed. This approach maintains a consistent view lifecycle and prevents exceptions caused by attempting to access disposed views.Tested the behaviour in the following platforms
Issues Fixed
Fixes https://github.com/dotnet/maui/issues/25724
Screenshots