MAUI Community Toolkit raise an error when close a popup

431 Views Asked by At

In my MAUI application, I want to display a nice message to inform the user that the application is loading the data. I added the CommunityToolkit.Maui and am using the Popup function.

I define a new toolkit:popup page. In another page, when I read the data, I added

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

    var popup = new LoadingPopup();
    this.ShowPopup(popup);

    await vm.Init();

    await popup.CloseAsync();
}

When I want to close the popup, I call the Close or CloseAsync property but in both cases I get this error

Object reference not set to an instance of an object.

at CommunityToolkit.Maui.Views.Popup.CommunityToolkit.Maui.Core.IPopup.OnClosed(Object result) at CommunityToolkit.Maui.Views.Popup.d__62.MoveNext() at CommunityToolkit.Maui.Views.Popup.d__60.MoveNext() at LanguageInUse.Views.DictionaryList.d__3.MoveNext() in C:\Projects\ERDevOps\LIUApp\LanguageInUse\Views\DictionaryList.xaml.cs:line 32

enter image description here

If I inspect the popup object, I see it is populated.

Update

The XAML of the LoadingPopup is

<toolkit:Popup xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
               xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
               xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
               x:Class="LanguageInUse.Views.Popups.LoadingPopup">

    <VerticalStackLayout WidthRequest="300" Margin="30" 
        VerticalOptions="CenterAndExpand">
        <ActivityIndicator IsRunning="True" Margin="15" />
        <Label Text="Loading..." Style="{StaticResource labelLoading}" />
    </VerticalStackLayout>
</toolkit:Popup>

and the code behind is

using CommunityToolkit.Maui.Views;

namespace LanguageInUse.Views.Popups;

public partial class LoadingPopup : Popup
{
    public LoadingPopup()
    {
        InitializeComponent();
    }
}
2

There are 2 best solutions below

0
On

Your code works well on my side, please make sure you have added Toolkit usage to app builder

var builder = MauiApp.CreateBuilder();
        builder
            .UseMauiApp<App>()
            .UseMauiCommunityToolkit() // HERE
            .ConfigureFonts(fonts =>
            {
                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
            });

Update: Oh, just make sure that your Popup have enough time to be initialized, your init() method is too fast.

0
On

It happens because you're closing your popup too early. ShowPopup() is a fire-and-forget method, so it's not guaranteed that the popup will be completely initialized after the call.

Try adding a delay before calling CloseAsync().

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

    var popup = new LoadingPopup();
    this.ShowPopup(popup);

    await vm.Init();

    // add this
    await Task.Delay(5000);

    await popup.CloseAsync();
}