Search code examples
popupmauimaui-community-toolkit

Community Toolkit Maui Popup Crashes When Clicking Outside


Community Toolkit Maui Popup crash when clicking outside of the dialog. But this happens only if the popup contains an Entry control.

See the sample at GitHub: https://github.com/HaimoHeymann/MauiTestApp

Any solution? Thanks!


Solution

  • When you click outside the popup you return a null and it is expecting an bool. So you get an

    Object reference not set to an instance of an object

    So your Event should look something like this by removing the explicit casting (bool):

    private async void OnTestClicked(object sender, EventArgs e)
    {
        var popup = new PopupEdit("Title", "", "Apply", "Cancel");
        var result = await Application.Current.MainPage.ShowPopupAsync(popup);
        if (result is bool boolResult)
        {
            if( boolResult)
            {
                lbResult.Text = popup.ResultString;
                return;
            }
        }
    
        lbResult.Text = "No Result";
    }
    

    If you do want to keep the explicit casting you can make it nullable like:

    var result = (bool?)await Application.Current.MainPage.ShowPopupAsync(popup);