Skip to content

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

Description

@TheCodeTraveler

Is there an existing issue for this?

  • I have searched the existing issues

Did you read the "Reporting a bug" section on Contributing file?

Current Behavior

When using a MediaElement in either CollectionViewHandler2 or CarouselViewHandler2 (introduced in .NET 9 to improve CollectionView and CarouselView performance on iOS), a InvalidCastException is thrown:

System.InvalidCastException: Specified cast is not valid.
   at CommunityToolkit.Maui.Core.Views.MauiMediaElement.<.ctor>g__GetInternalControllerForItemsView2|0_1[ReorderableItemsView](ItemsViewHandler2`1 handler) in /_/src/CommunityToolkit.Maui.MediaElement/Views/MauiMediaElement.macios.cs:line 92
   at CommunityToolkit.Maui.Core.Views.MauiMediaElement..ctor(AVPlayerViewController playerViewController, MediaElement virtualView) in /_/src/CommunityToolkit.Maui.MediaElement/Views/MauiMediaElement.macios.cs:line 68
   at CommunityToolkit.Maui.Core.Handlers.MediaElementHandler.CreatePlatformView() in /_/src/CommunityToolkit.Maui.MediaElement/Handlers/MediaElementHandler.macios.cs:line 28
   at Microsoft.Maui.Handlers.ViewHandler`2[[CommunityToolkit.Maui.Views.MediaElement, CommunityToolkit.Maui.MediaElement, Version=6.1.3.0, Culture=neutral, PublicKeyToken=null],[CommunityToolkit.Maui.Core.Views.MauiMediaElement, CommunityToolkit.Maui.MediaElement, Version=6.1.3.0, Culture=neutral, PublicKeyToken=null]].OnCreatePlatformView()
   at Microsoft.Maui.Handlers.ViewHandler.OnCreatePlatformElement()
   at Microsoft.Maui.Handlers.ElementHandler.CreatePlatformElement()
   at Microsoft.Maui.Handlers.ElementHandler.SetVirtualView(IElement view)
   at Microsoft.Maui.Handlers.ViewHandler`2[[CommunityToolkit.Maui.Views.MediaElement, CommunityToolkit.Maui.MediaElement, Version=6.1.3.0, Culture=neutral, PublicKeyToken=null],[CommunityToolkit.Maui.Core.Views.MauiMediaElement, CommunityToolkit.Maui.MediaElement, Version=6.1.3.0, Culture=neutral, PublicKeyToken=null]].SetVirtualView(IView view)
   at Microsoft.Maui.Handlers.ViewHandler`2[[CommunityToolkit.Maui.Views.MediaElement, CommunityToolkit.Maui.MediaElement, Version=6.1.3.0, Culture=neutral, PublicKeyToken=null],[CommunityToolkit.Maui.Core.Views.MauiMediaElement, CommunityToolkit.Maui.MediaElement, Version=6.1.3.0, Culture=neutral, PublicKeyToken=null]].SetVirtualView(IElement view)
   at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler newHandler)
   at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value)
   at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.set_Handler(IElementHandler value)
   at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement view, IMauiContext context)
   at Microsoft.Maui.Platform.ElementExtensions.ToPlatform(IElement view, IMauiContext context)
   at Microsoft.Maui.Handlers.LayoutHandler.SetVirtualView(IView view)
   at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.ILayout, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Platform.LayoutView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].SetVirtualView(IElement view)
   at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler newHandler)
   at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value)
   at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.set_Handler(IElementHandler value)
   at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement view, IMauiContext context)
   at Microsoft.Maui.Platform.ElementExtensions.ToPlatform(IElement view, IMauiContext context)
   at Microsoft.Maui.Handlers.BorderHandler.UpdateContent(IBorderHandler handler)
   at Microsoft.Maui.Handlers.BorderHandler.MapContent(IBorderHandler handler, IBorderView border)
   at Microsoft.Maui.PropertyMapper`2.<>c__DisplayClass5_0[[Microsoft.Maui.IBorderView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Handlers.IBorderHandler, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].<Add>b__0(IElementHandler h, IElement v)
   at Microsoft.Maui.PropertyMapper.UpdateProperties(IElementHandler viewHandler, IElement virtualView)
   at Microsoft.Maui.Handlers.ElementHandler.SetVirtualView(IElement view)
   at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.IBorderView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Platform.ContentView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].SetVirtualView(IView view)
   at Microsoft.Maui.Handlers.BorderHandler.SetVirtualView(IView view)
   at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.IBorderView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Platform.ContentView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].SetVirtualView(IElement view)
   at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler newHandler)
   at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value)
   at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.set_Handler(IElementHandler value)
   at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement view, IMauiContext context)
   at Microsoft.Maui.Platform.ElementExtensions.ToPlatform(IElement view, IMauiContext context)
   at Microsoft.Maui.Controls.Handlers.Items2.TemplatedCell2.BindVirtualView(View virtualView, Object bindingContext, ItemsView itemsView, Boolean needsContainer)
   at Microsoft.Maui.Controls.Handlers.Items2.TemplatedCell2.Bind(DataTemplate template, Object bindingContext, ItemsView itemsView)
   at Microsoft.Maui.Controls.Handlers.Items2.ItemsViewController2`1[[Microsoft.Maui.Controls.ReorderableItemsView, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].GetCell(UICollectionView collectionView, NSIndexPath indexPath)
   at UIKit.UIApplication.UIApplicationMain(Int32 argc, String[] argv, IntPtr principalClassName, IntPtr delegateClassName) in /Users/builder/azdo/_work/1/s/macios/src/UIKit/UIApplication.cs:line 69
   at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass) in /Users/builder/azdo/_work/1/s/macios/src/UIKit/UIApplication.cs:line 104
   at CollectionViewHandler2BugRepro.Program.Main(String[] args) in /Users/brandon/GitHub/CollectionViewHandler2BugRepro/CollectionViewHandler2BugRepro/Platforms/iOS/Program.cs:line 13

Expected Behavior

Developers should be able to use MediaElement in CarouselView and CollectionView with the new performant ItemsView2 handlers.

Steps To Reproduce

  1. Clone the linked reproduction sample: https://github.com/TheCodeTraveler/CollectionViewHandler2BugRepro
  2. Open CollectionViewHandler2BugRepro.csproj in either Visual Studio or Jet Brains Rider
  3. Build/run CollectionViewHandler2BugRepro on iOS
  4. When the app launches, confirm the app crashes with an InvalidCastException

Link to public reproduction project repository

https://github.com/TheCodeTraveler/CollectionViewHandler2BugRepro

Environment

- .NET MAUI CommunityToolkit MediaElement: v6.1.3
- OS: iOS + MacCatalyst
- .NET MAUI: 9.0.120

Anything else?

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions