CollectionView with EmptyView and EmptyViewTemplate for Data template selector throws an exception
Root Cause
The exception occurred because the RealizeView method in the ItemsViewController file was attempting to create and render a view when both EmptyView and EmptyViewTemplate were defined in the CollectionView, with a DataTemplateSelector for the EmptyViewTemplate. The original code did not account for the scenario where the viewTemplate was a DataTemplateSelector. In this case, the RealizeView method expected a DataTemplate to be directly provided, leading to a null reference issue when trying to use the viewTemplate. This caused an exception when it encountered a DataTemplateSelector, which should dynamically choose the correct template based on the input data.
Description of Change
The fix introduced an additional condition to handle cases where the viewTemplate is a DataTemplateSelector. This ensures that when the view is null and the viewTemplate is either null or a DataTemplateSelector, the logic correctly bypasses attempting to realize a non-existent view. This fix code prevents the exception by ensuring that the method can handle DataTemplateSelector cases, avoiding invalid template realization, and allowing the CollectionView to display the appropriate empty view template without errors.
Tested the behaviour in the following platforms
- [x] Android
- [x] Windows
- [x] iOS
- [x] Mac
Issues Fixed
Fixes https://github.com/dotnet/maui/issues/25224
Screenshots
iOS:
| Before Issue Fix | After Issue Fix |
|---|---|
Mac:
| Before Issue Fix | After Issue Fix |
|---|---|
/azp run
Azure Pipelines successfully started running 3 pipeline(s).
/azp run
Azure Pipelines successfully started running 3 pipeline(s).
/rebase
/azp run
Azure Pipelines successfully started running 3 pipeline(s).
/rebase
/azp run
Azure Pipelines successfully started running 3 pipeline(s).
/azp run
Azure Pipelines successfully started running 3 pipeline(s).
/rebase
/azp run
Azure Pipelines successfully started running 3 pipeline(s).
/rebase
/azp run
Azure Pipelines successfully started running 3 pipeline(s).
/rebase
/azp run
Azure Pipelines successfully started running 3 pipeline(s).
/azp run
Azure Pipelines successfully started running 3 pipeline(s).
/azp run
Azure Pipelines successfully started running 3 pipeline(s).
/rebase
/azp run
Azure Pipelines successfully started running 3 pipeline(s).