Closed TimLariviere closed 1 year ago
@TimLariviere - I was just about to post the same issue! :) It appears to be, as you say, iOS only related and I took the standard template project (with shell) and added MediaElement to MainPage and all works fine but I then replace the line in app "MainPage = new AppShell()" with "MainPage = new MainPage()" (or with NavigationPage) the expection occurs.
p.s. Not sure that workaround works if the MediaElement is in XAML?
I wonder if we need to implement something similar to #934 where we check whether IsPlatformEnabled
before trying to take action.
This seems to be actually a bigger problem than first thought as having tried numerous scenarios it appears you can't use the MediaElement outside of a Shell based app (iOS at least) as trying to use it anywhere in a NavigationPage based app you get exceptions either on PushAsync to a page with a MediaElement or when PopAsync which cause issues with the page popped to. I suspect that there's an internal reliance or reference to Shell somewhere (perhaps main window?) and that it's just not possible to use it in a non-Shell app .. does anyone know if this is the case?
Closing this in favor of #985
Is there an existing issue for this?
Did you read the "Reporting a bug" section on Contributing file?
Current Behavior
When adding a
MediaElement
to aContentPage
used as the MainPage ofApplication
(noShell
), the app crashes on iOS at startup because of anInvalidOperationException
. Android seems fine.I tracked it down to
WindowStateManager.Default.GetCurrentUIViewController()
returning null. https://github.com/CommunityToolkit/Maui/blob/e6f9e9180e8d6141c73752caaf19597b32c91492/src/CommunityToolkit.Maui.MediaElement/Views/MauiMediaElement.macios.cs#L24This is not the case when using
Shell
, so I believe the creation of the MediaElement is somehow delayed when using Shell, giving time for WindowStateManager to be initialized, but not when using ContentPage directly.Expected Behavior
The app should run fine on iOS, just like it works already on Android.
Steps To Reproduce
.UseMauiCommunityToolkitMediaElement()
to the MauiApp builderA workaround is available in the project below
Link to public reproduction project repository
https://github.com/TimLariviere/CommunityToolkit-Maui-MediaElement-Crash
Environment
Anything else?
No response