I am trying to use the .Net Maui community toolkit to show a Popup as a loading page when computing in page and between pages. However when I execute the code it does not show the popup on the device using this.ShowPopUpAsync(popup) or Application.Current.MainPage.ShowPopupAsync(popup).
My PopUp Xaml is
<?xml version="1.0" encoding="utf-8" ?>
<xct:Popup xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="DailyBudgetMAUIApp.Handlers.PopUpPage"
xmlns:xct="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
CanBeDismissedByTappingOutsideOfPopup="False"
Color="Transparent">
<VerticalStackLayout HeightRequest="300" WidthRequest="200">
<ActivityIndicator IsRunning="True" IsVisible="True" VerticalOptions="StartAndExpand" HorizontalOptions="Center" BackgroundColor="Transparent" Margin="0,100,0,0" Color="{DynamicResource Primary}" Scale="1">
</ActivityIndicator>
<Label Text="Loading ..." FontAttributes="Bold" FontSize="Medium" VerticalOptions="Center" HorizontalOptions="Center" FontFamily="OpenSansSemibold" TextColor="{DynamicResource Light}" Margin="0,20,0,0"/>
</VerticalStackLayout>
</xct:Popup>
With the Xaml.cs
public partial class PopUpPage : Popup
{
public PopUpPage()
{
InitializeComponent();
}
}
And then I am calling a method where I want to show the popup in something like this
private async void UndoCreateBudget(int BudgetID)
{
Popup popup = new PopUpPage();
await Application.Current.MainPage.ShowPopupAsync(popup);
//Call my api and do some stuff
popup.Close();
}
I have tested your code. The Popup is valid. And using this way to call it - it will show up.
Tested it on Android and Windows. I am almost certain that if you place a breakpoint in this method:
UndoCreateBudget
it will never get hit at first place. So you should check this first.Anyway, I am not writing this to tell you it will work. You have a deadlock right there.
This line:
Will very tricky lure you to await it, when you hover over it. And it does not take into consideration that you have this line in the xaml:
You have to run this task. Run it, not await it. It waits for the popup to close. And this kind of popup you have will never close on its own.
You will never get to the API call. And you will lock your interface.