Skip to content
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

CollectionView with EmptyView and EmptyViewTemplate for Data template selector throws an exception #25418

Merged
merged 16 commits into from
Nov 22, 2024

Conversation

Vignesh-SF3580
Copy link
Contributor

@Vignesh-SF3580 Vignesh-SF3580 commented Oct 21, 2024

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

  • Android
  • Windows
  • iOS
  • Mac

Issues Fixed

Fixes #25224

Screenshots

iOS:

Before Issue Fix After Issue Fix
)

Mac:

Before Issue Fix After Issue Fix

@dotnet-policy-service dotnet-policy-service bot added the community ✨ Community Contribution label Oct 21, 2024
@jsuarezruiz
Copy link
Contributor

/azp run

@jsuarezruiz jsuarezruiz added the area-controls-collectionview CollectionView, CarouselView, IndicatorView label Oct 21, 2024
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@jsuarezruiz
Copy link
Contributor

/azp run

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@Vignesh-SF3580 Vignesh-SF3580 marked this pull request as ready for review October 22, 2024 13:46
@Vignesh-SF3580 Vignesh-SF3580 requested a review from a team as a code owner October 22, 2024 13:46
@jsuarezruiz
Copy link
Contributor

/rebase

@jsuarezruiz
Copy link
Contributor

/azp run

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@PureWeen
Copy link
Member

/rebase

@PureWeen
Copy link
Member

/azp run

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

Copy link
Contributor

@jsuarezruiz jsuarezruiz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests are failing on iOS and Catalyst

image

@PureWeen PureWeen added this to the .NET 9 SR1 milestone Oct 31, 2024
@jsuarezruiz
Copy link
Contributor

/azp run

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@praveenkumarkarunanithi
Copy link
Contributor

/rebase

@jsuarezruiz
Copy link
Contributor

/azp run

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@jsuarezruiz jsuarezruiz self-assigned this Nov 6, 2024
@jsuarezruiz jsuarezruiz self-requested a review November 7, 2024 11:33
@@ -567,7 +567,7 @@ protected virtual void HandleFormsElementMeasureInvalidated(VisualElement formsE
internal void UpdateView(object view, DataTemplate viewTemplate, ref UIView uiView, ref VisualElement formsElement)
{
// Is view set on the ItemsView?
if (view is null && viewTemplate is null)
if (view is null && (viewTemplate is null || viewTemplate is DataTemplateSelector))
Copy link
Contributor

@jsuarezruiz jsuarezruiz Nov 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the PR description:

"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 the case of using a DataTemplateSelector, here

viewTemplate = viewTemplate.SelectDataTemplate(view, itemsView);

should select the correct template, and then get the content to create the handler and the PlatformView.

Is the SelectTemplate method returning null?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jsuarezruiz ,
In this case, the SelectTemplate method itself isn't returning null. Instead, the exception occurs immediately afterward, when the selected viewTemplate is used in CreateContent. This happens because, on the initial render, there's no template available to select, resulting in viewTemplate being null. This leads to an exception when CreateContent is called with an empty or unassigned DataTemplateSelector.

Let me know if further clarification is needed!

@PureWeen
Copy link
Member

/azp run

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@PureWeen PureWeen modified the milestones: .NET 9 SR1.1, .NET 9 SR2 Nov 20, 2024
@PureWeen PureWeen dismissed stale reviews from jsuarezruiz and themself November 22, 2024 16:15

Changes Addressed

@PureWeen PureWeen merged commit f816ca4 into dotnet:main Nov 22, 2024
108 checks passed
@sheiksyedm sheiksyedm added the partner/syncfusion Issues / PR's with Syncfusion collaboration label Dec 6, 2024
@samhouts samhouts added fixed-in-9.0.21 fixed-in-net8.0-nightly This may be available in a nightly release! labels Dec 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-controls-collectionview CollectionView, CarouselView, IndicatorView community ✨ Community Contribution fixed-in-9.0.21 fixed-in-net8.0-nightly This may be available in a nightly release! partner/syncfusion Issues / PR's with Syncfusion collaboration
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

[MAUI]Clicking the "I5_EmptyView with EmptyViewTemplate for Data template selector" page throws an exception
8 participants