I'm working on an application that should open a popup while processing some images. The problem is that the popup does not open. In some tests I've done, the popup only opens after all processing has completed, even then if I put Thread.Sleep() to wait before closing. I have the following code in my maui .net application. This code should open the popup at the beginning of image conversion processing. And then at the end it's called again to close the popup. This is the code for my popup controller.
WaitPopup.xaml
<mct:Popup xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:mct="clr-namespace:CommunityToolkit.Maui.Views;assembly=CommunityToolkit.Maui"
xmlns:viewmodel="clr-namespace:ImageConvert.ViewModel"
x:DataType="viewmodel:GifAnimationViewModel"
x:Class="ImageConvert.Views.Popups.WaitPopup"
CanBeDismissedByTappingOutsideOfPopup="False">
<VerticalStackLayout>
<Label
Text="Wait minute!"
VerticalOptions="Center"
HorizontalOptions="Center" />
<Image Source="Resources/Images/loading.gif" IsAnimationPlaying="{Binding IsAnimationGif }" WidthRequest="150"/>
</VerticalStackLayout>
</mct:Popup>
WaitPopup.xaml.cs
using CommunityToolkit.Maui.Views;
namespace ImageConvert.Views.Popups;
public partial class WaitPopup : Popup
{
public WaitPopup()
{
InitializeComponent();
}
}
This is the viewmodel code that does a pre-processing and sends a message to open the popup and calls the image processing service.
ConvertViewModel.cs
public ICommand ConverterCommand => new Command(ConverterImages);
private void ConverterImages(object obj)
{
try
{
MainThread.BeginInvokeOnMainThread(() =>
{
var folder = string.Empty;
switch (FormatSelected)
{
case "jpeg":
folder = FoldersNames.NamesFolders[0];
break;
case "pdf":
folder = FoldersNames.NamesFolders[1];
break;
case "png":
folder = FoldersNames.NamesFolders[2];
break;
case "bmp":
folder = FoldersNames.NamesFolders[3];
break;
case "tiff":
folder = FoldersNames.NamesFolders[4];
break;
case "webp":
folder = FoldersNames.NamesFolders[5];
break;
case "gif":
folder = FoldersNames.NamesFolders[6];
break;
default:
folder = FoldersNames.NamesFolders[2];
break;
}
string folderSavePath = Path.Combine(FoldersNames.PathRoot, folder);
var listPaths = (from i in ListImages select i.PathImage).ToList();
var send = new SendMessageToDisplayWaitWindow()
{
OpenOrCloseDisplay = true,
ListImages = listPaths,
FolderSave = folderSavePath,
FormatSelect = FormatSelected
};
WeakReferenceMessenger.Default.Send<SendMessageToDisplayWaitWindow>(send);
});
}
catch (Exception ex)
{
Console.WriteLine("***** " + ex.Message);
}
}
Then the code comes to this part, where it should open the popup and call the image processing service.
Convert.xaml.cs
private IConverterImage _converter;
public Convert(ConvertViewModel pConvertViewModel, IConverterImage pConverter)
{
InitializeComponent();
_converter = pConverter;
BindingContext = pConvertViewModel;
this.Loaded += Convert_Loaded;
WeakReferenceMessenger.Default.Register(this, (MessageHandler<object, SendMessageToDisplayWaitWindow>)((e, msg) =>
{
LoadUnLoadDisplay(msg);
}));
}
private void LoadUnLoadDisplay(SendMessageToDisplayWaitWindow msg)
{
try
{
MainThread.BeginInvokeOnMainThread(() =>
{
var popup = Handler.MauiContext.Services.GetService<WaitPopup>();
if (msg.OpenOrCloseDisplay)
{
this.ShowPopup(popup);
_converter.ProcessImages(msg.ListImages, msg.FolderSave, msg.FormatSelect);
}
else
{
popup.Close();
}
});
}
catch (Exception ex)
{
Console.WriteLine(string.Format("**** \n {0}", ex.Message));
}
}
I don't understand what could be causing this bug, nor what to do to solve it.