-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Toggling Header/Footer in CollectionView Dynamically throws an exception #25743
Conversation
… in CollectionView Dynamically
Hey there @Vignesh-SF3580! 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 |
Azure Pipelines successfully started running 3 pipeline(s). |
/azp run |
Azure Pipelines successfully started running 3 pipeline(s). |
@@ -577,7 +577,7 @@ internal void UpdateView(object view, DataTemplate viewTemplate, ref UIView uiVi | |||
|
|||
uiView?.Dispose(); | |||
uiView = null; | |||
|
|||
formsElement?.Handler?.DisconnectHandler(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this fix on ItemsViewController2 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rmarinho ,
I've tested this scenario with CollectionViewHandler2, and since the header and footer handling differs in that implementation, the issue does not occur. Dynamically setting the header, footer, and empty view to null works without any exceptions in CollectionViewHandler2, so this fix isn't needed there.
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 #25724
Screenshots
BeforeFix.mov
Afterfix.mov