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
22.26k stars 1.76k forks source link

Toggling Header/Footer in CollectionView Dynamically throws an exception #25743

Closed Vignesh-SF3580 closed 4 days ago

Vignesh-SF3580 commented 2 weeks ago

Root Cause

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.

Tested the behaviour in the following platforms

Issues Fixed

Fixes https://github.com/dotnet/maui/issues/25724

Screenshots

Before Issue Fix After Issue Fix
dotnet-policy-service[bot] commented 2 weeks ago

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.

jsuarezruiz commented 1 week ago

/azp run

azure-pipelines[bot] commented 1 week ago
Azure Pipelines successfully started running 3 pipeline(s).
jsuarezruiz commented 1 week ago

/azp run

azure-pipelines[bot] commented 1 week ago
Azure Pipelines successfully started running 3 pipeline(s).