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
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();
}
}
Your code works well on my side, please make sure you have added Toolkit usage to app builder
Update: Oh, just make sure that your Popup have enough time to be initialized, your init() method is too fast.