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?

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

  1. Create a Popup in you’re application
  2. Add a button that opens the popup
  3. Start screen mirroring on the device (i’ve tested it on an iPad with iOS 16.4)
  4. Open the app
  5. Click the button that opens the popup
  6. 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)

Most upvoted comments

But first I want to express frustration that it took less than 2 weeks for DevExpress to have a hotfix for the same issue. By using a decompiler, I know they added a single expression, a ~20 character change, and now all of their controls are immune to this problem. Meanwhile, this issue has been ignored for a year by the MAUI/Toolkit teams.

@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 IWindowStateManager that properly selects the correct UIWindow and UIViewController, it seems you can use WindowStateManager.Default.Init and pass in a delegate for GetCurrentUIViewController that will be favored over the default implementation.

My personal approach was to sort the UIWindowScene’s by their UIWindowSceneSessionRole:

using var scenes = UIApplication.SharedApplication.ConnectedScenes;
var windowScene = scenes.ToArray<UIWindowScene>()
                        .MinBy(scene => (int)scene.Session.Role); // favor "Application" over other roles.
return windowScene?.Windows.FirstOrDefault();

This works because Application has a value of 0, so all the others, like ExternalDisplay (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 !