Skip to content

Conversation

@TheCodeTraveler
Copy link
Collaborator

@TheCodeTraveler TheCodeTraveler commented Nov 4, 2025

Description of Change

This PR fixes a typo I made when adding support for ItemsViewController2<T> in MauiMediaElement.macios.cs.

When a developer is using an ItemsViewController2<T>, we were incorrectly casting it to ItemsViewController<T> instead of a ItemsViewController2<T>, causing the InvalidCastException.

Linked Issues

PR Checklist

  • Has a linked Issue, and the Issue has been approved(bug) or Championed (feature/proposal)
  • Rebased on top of main at time of PR
  • Changes adhere to coding standard

Additional information

The bug was due to a copy/paste error when I created the following two methods side-by-side:

  • static ItemsViewController<TItemsView> GetInternalControllerForItemsView<TItemsView>(ItemsViewHandler<TItemsView>) where TItemsView : ItemsView
  • static ItemsViewController2<TItemsView> GetInternalControllerForItemsView2<TItemsView>(ItemsViewHandler2<TItemsView>) where TItemsView : ItemsView

This bug was not caught earlier because the Controller we need from .NET MAUI is a protected internal property, forcing us to use reflection to retrieve it. If the property was public we could rely on C# type-safety.

Copy link
Member

@ne0rrmatrix ne0rrmatrix left a comment

Choose a reason for hiding this comment

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

Looks good. I am currently testing it. Have you thought about adding a sample to sample app to track behavior?

@TheCodeTraveler
Copy link
Collaborator Author

Have you thought about adding a sample to sample app to track behavior

That's the weird thing! CommunityToolkit.Maui.Sample is already configured to use CollectionViewHandler2 and CarouselViewHandler2:

#if IOS || MACCATALYST
handlers.AddHandler<CollectionView, Microsoft.Maui.Controls.Handlers.Items2.CollectionViewHandler2>();
handlers.AddHandler<CarouselView, Microsoft.Maui.Controls.Handlers.Items2.CarouselViewHandler2>();
#endif

However, when I debug the sample app and load the MediaElementCollectionViewPage, the debugger shows it is still using CollectionViewHandler and not CollectionViewHandler2. I couldn't figure out why it's still using the older CollectionViewHandler instead of using the CollectionViewHandler2 that we initialized in MauiProgram.

Copy link
Member

@ne0rrmatrix ne0rrmatrix left a comment

Choose a reason for hiding this comment

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

My only suggestion at this point is to create a new page for media element that uses CarouselView that supports Mac/iOS. Currently we have a CarouselView page but it only supports Windows/Android and uses multiple itemViews which is not currently supported on Mac/iOS. If we include that I would be happy to approve this. As far as I can tell it works. I tested the PR branch on my pac in a simulator and it works.

Good catch btw. If you feel we do not need that page go ahead and merge this as it does exactly what we need it to. It works.

@TheCodeTraveler
Copy link
Collaborator Author

Thanks! I say we defer the sample app updates since you've already opened a PR to implement the fix for multiple ItemsViews: #2807

@TheCodeTraveler TheCodeTraveler merged commit 778c618 into main Nov 4, 2025
11 checks passed
@TheCodeTraveler TheCodeTraveler deleted the Fix-Support-for-ItemsViewController2-T- branch November 4, 2025 20:30
@github-actions github-actions bot locked and limited conversation to collaborators Nov 6, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] (MediaElement) InvalidCastException thrown when using MediaElement in CollectionViewHandler2 and CarouselViewHandler2

2 participants