Maui: [BUG] iOS screen mirroring with popup let the app crash on opening the popup
Is there an existing issue for this?
- I have searched the existing issues
Did you read the “Reporting a bug” section on Contributing file?
- I have read the “Reporting a bug” section on Contributing file: https://github.com/CommunityToolkit/Maui/blob/main/CONTRIBUTING.md#reporting-a-bug
Current Behavior
When opening a Popup when screen mirroring is active. The app crashes with a System.InvalidOperationException.
System.InvalidOperationException: ViewController cannot be null. at CommunityToolkit.Maui.Core.Views.MauiPopup.SetElement(IPopup element) in /_/src/CommunityToolkit.Maui.Core/Views/Popup/MauiPopup.macios.cs:line 80 at CommunityToolkit.Maui.Core.Handlers.PopupHandler.ConnectHandler(MauiPopup platformView) in /_/src/CommunityToolkit.Maui.Core/Handlers/Popup/PopupHandler.macios.cs:line 91 at Microsoft.Maui.Handlers.ElementHandler2[[CommunityToolkit.Maui.Core.IPopup, CommunityToolkit.Maui.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[CommunityToolkit.Maui.Core.Views.MauiPopup, CommunityToolkit.Maui.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].OnConnectHandler(Object platformView)
at Microsoft.Maui.Handlers.ElementHandler.ConnectHandler(Object platformView)
at Microsoft.Maui.Handlers.ElementHandler.SetVirtualView(IElement view)
at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler newHandler)
at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value)
at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement view, IMauiContext context)
at CommunityToolkit.Maui.Views.PopupExtensions.CreatePopup(Page page, Popup popup) in //src/CommunityToolkit.Maui/Views/Popup/PopupExtensions.shared.cs:line 89
at CommunityToolkit.Maui.Views.PopupExtensions.CreateAndShowPopupAsync[ToolkitPopupPage](Page page, ToolkitPopupPage popup) in //src/CommunityToolkit.Maui/Views/Popup/PopupExtensions.shared.cs:line 113
at CommunityToolkit.Maui.Views.PopupExtensions.ShowPopupAsync[ToolkitPopupPage](Page page, ToolkitPopupPage popup) in /_/src/CommunityToolkit.Maui/Views/Popup/PopupExtensions.shared.cs:line 57
at MauiApp1.MainPage.OnOpenToolkitPopupPage(Object sender, EventArgs e) in /Users/user/RiderProjects/MauiApp1/MauiApp1/MainPage.xaml.cs:line 24
at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_0(Object )
at Foundation.NSAsyncSynchronizationContextDispatcher.Apply()
at UIKit.UIApplication.UIApplicationMain(Int32 , String[] , IntPtr , IntPtr )
at UIKit.UIApplication.Main(String[] , Type , Type )
at MauiApp1.Program.Main(String[] args) in /Users/user/RiderProjects/MauiApp1/MauiApp1/Platforms/iOS/Program.cs:line 13`
Expected Behavior
The popup should behave the same as on the device. This is very annoying when demo MAUI apps when the app crashes.
Steps To Reproduce
- Create a Popup in you’re application
- Add a button that opens the popup
- Start screen mirroring on the device (i’ve tested it on an iPad with iOS 16.4)
- Open the app
- Click the button that opens the popup
- The app crashes…
Link to public reproduction project repository
https://github.com/imuller/app-popups-problem
Environment
- .NET MAUI CommunityToolkit: 5.1.0
- OS: iOS 16.4 running on and iPad Pro
- .NET MAUI: 7.0.81
Anything else?
No response
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 1
- Comments: 19 (6 by maintainers)
@zeldafreak I hope you understand that every maintainer on the Community Toolkit team is a volunteer. None of us get paid for the hundreds of hours we have contributed to this project. We devote our spare time on our nights and weekends to help fellow .NET MAUI developers.
We are community members just like you. Everyone in the community, including you, is equally capable of submitting PRs, fixing bugs and creating new features.
We aren’t ignoring Issues. We aren’t ignoring Pull Requests. Our time is limited to the hours we’re willing to dedicate away from our families to help create this library to benefit developers like you.
I encourage you to submit a Pull Request with your fix.
I think I was able to work around this issue thanks to @softlion and this PR over here: https://github.com/rotorgames/Rg.Plugins.Popup/pull/754
While we can’t provide our own implementation of
IWindowStateManagerthat properly selects the correctUIWindowandUIViewController, it seems you can useWindowStateManager.Default.Initand pass in a delegate forGetCurrentUIViewControllerthat will be favored over the default implementation.My personal approach was to sort the
UIWindowScene’s by theirUIWindowSceneSessionRole:This works because
Applicationhas a value of0, so all the others, likeExternalDisplay(which is the role of the AirPlay scene) will always come after it.I have no idea if I will run into some other kind of issue in the future by doing this-- I’m not an iOS developer. But if I’m lucky I won’t be humiliated by intermittent crashes when demonstrating progress on my project like I was this morning.
Thank you so much, @softlion and @EmilienDup !