Skip to content

[BUG] Popup: PopupNotFoundException is thrown when dismissing a popup opened from a Modal NavigationPage #3148

Description

@EmilienDup

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

Dismissing a popup by clicking outside of it will throw a PopupNotFoundException when the popup has been opened from a NavigationPage on the modal stack:

Expected Behavior

Dismissing the popup should work just fine

Steps To Reproduce

Run this code, after the popup appears, just click outside the popup to dismiss it

public class MainPage : ContentPage
{
    protected override void OnAppearing()
    {
        base.OnAppearing();
        
        this.Navigation.PushModalAsync(
            new NavigationPage(
                new PopupLauncherContentPage()));
    }
}

public class PopupLauncherContentPage : ContentPage
{
    private bool _appeared = false;

    protected override void OnAppearing()
    {
        base.OnAppearing();

        if (this._appeared)
        {
            return;
        }

        var popup = new Popup()
        {
            Content = new Label { Text = "Hello Popup!" },
            CanBeDismissedByTappingOutsideOfPopup = true
        };

        this.Navigation.ShowPopup(popup);
        this._appeared = true;
    }
}

Exception thrown

CommunityToolkit.Maui.Views.PopupNotFoundException: Unable to close popup: could not locate PopupPage. ShowPopup or ShowPopupAsync must be called before CloseAsync. If using a custom implementation of Popup, override the CloseAsync method
   at CommunityToolkit.Maui.Views.PopupPage.CloseAsync(PopupResult result, CancellationToken token) in /_/src/CommunityToolkit.Maui/Views/Popup/PopupPage.shared.cs:line 83
   at CommunityToolkit.Maui.Views.PopupPage.<>c__DisplayClass4_0.<<-ctor>b__0>d.MoveNext() in /_/src/CommunityToolkit.Maui/Views/Popup/PopupPage.shared.cs:line 46
--- End of stack trace from previous location ---
   at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__124_0(Object state)
   at Android.App.SyncContext.<>c__DisplayClass2_0.<Post>b__0()
   at Java.Lang.Thread.RunnableImplementor.Run()
   at Java.Lang.IRunnableInvoker.n_Run(IntPtr jnienv, IntPtr native__this)

Link to public reproduction project repository

Sample provided in the ticket description

Environment

- .NET MAUI CommunityToolkit:14.0.1
- OS: MacOS
- .NET MAUI:10.0.50

Anything else?

May relate to #2839

Metadata

Metadata

Labels

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions