Maui icon indicating copy to clipboard operation
Maui copied to clipboard

[BUG] Popup V2 May Not Work If Invoked Via `OnAppearing`

Open david-maw opened this issue 8 months ago • 4 comments

Is there an existing issue for this?

  • [x] I have searched the existing issues

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

  • [x] 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

If (as I did) you have an async initialization method that's awaited by an OnAppearing method, it may fail to show a Popup V2 (the earlier Popup implementation worked fine). Instead, the await on PopupAsync may exit immediately, or may hang indefinitely. In the sample provided it exits immediately without showing a popup.

Expected Behavior

The popup should show and wait to be manually dismissed.

Steps To Reproduce

  1. Download the repository from GitHub.
  2. Open, build and run the project.

Link to public reproduction project repository

https://github.com/david-maw/PopupPage.git

Environment

- .NET MAUI CommunityToolkit: 1.2.0
- OS: Windows 11
- .NET MAUI: 9.0.80

Anything else?

The "main" branch demonstrates the bug; the "workaround" branch demonstrates the workaround which is just to introduce a 50ms delay before invoking the popup.

david-maw avatar Jun 23 '25 22:06 david-maw

@david-maw can you try to use the Laoded event instead?

pictos avatar Jun 23 '25 23:06 pictos

Just tried it, same problem, although Loaded does seem to solve the unfortunate recursion problem you get if you use a popup V2 in OnAppearing.

david-maw avatar Jun 24 '25 03:06 david-maw

I know we don't want to encourage showing a Popup from OnAppearing but there was a fix we had to do in order to get the first version to work in OnAppearing. I think it was around using the IsPlatformEnabled property. I'll have a look at this

bijington avatar Jun 24 '25 05:06 bijington

Thanks to @pictos suggestion to try the Loading event, i discovered it is not called recursively if it uses a popup, which might be a relatively easy solution to my woes.

david-maw avatar Jun 24 '25 16:06 david-maw

Correct, we recommend using Loaded event.

We have an example of how to show a Popup in the OnAppearing method in our sample app here:

https://github.com/CommunityToolkit/Maui/blob/28e030c24f370c73e6fb0490ba4111ad7c74f205/samples/CommunityToolkit.Maui.Sample/Pages/Views/Popup/ShowPopupInOnAppearingPage.xaml.cs#L1-L29

TheCodeTraveler avatar Jun 26 '25 19:06 TheCodeTraveler

It's probably worth documenting that popups now use navigation and that may require code changes if existing code doesn't expect it.

david-maw avatar Jun 26 '25 20:06 david-maw

To be clear, my issue was not the popup recursion via OnAppearing, it was that using async caused the popup to fail entirely (even via Loading). It's certainly possible to address that by not supporting some configuration, but if so, that ought to be documented.

david-maw avatar Jun 26 '25 23:06 david-maw