Home > Enterprise >  In .NET Maui for iOS, can you display an alert from a community Popup?
In .NET Maui for iOS, can you display an alert from a community Popup?

Time:01-27

I have a community toolkit popup (toolkit:Popup) that asks a question on a button press.

<toolkit:Popup xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:telerik="http://schemas.telerik.com/2022/xaml/maui"
         xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"             
         x:Class="PinPopUp">
    <VerticalStackLayout>
        <Button x:Name="btnRate" Text="Rate" FontSize="12" Clicked="btnRate_Clicked" 
            VerticalOptions="Center" HeightRequest="40" HorizontalOptions="Fill" Padding="8" />
    </VerticalStackLayout>
</toolkit:Popup>

with this code behind:

private async void btnRate_Clicked(object sender, EventArgs e)
{
    try
    {     
        bool answer = await App.Current.MainPage.DisplayAlert("Rating", message, "Yes", "No");
        if (answer)
        {         
            await App.Current.MainPage.DisplayAlert("Submitting...", result, "OK");
        }
    }
    catch (Exception ex)
    {
        Crashes.TrackError(ex);
        await App.Current.MainPage.DisplayAlert("Oops!", "There was an error rating this shop! =(", "OK");
    }
}

This works fine on Android, but on iOS once it hits the line to display the first alert, the code execution just returns to form and nothing appears. You can click the button repeatedly.

If it matters the Popup is called by a ContentView not a ContentPage.

I feel like the alert is appearing somewhere its just not visible on the screen, maybe behind something. Is there a way to ensure it is in front of everything?

CodePudding user response:

Your DisplayAlert call above should indeed be good for both Android and iOS; the only thing that might be in the way of you catching the response might be that iOS really recquires it to be running on the main ui thread. Maybe try and encompasse your asynchronous code inside this:

MainThread.BeginInvokeOnMainThread(() =>
{
    bool answer = await App.Current.MainPage.DisplayAlert("Rating", message, "Yes", "No");
    if (answer)
    {         
        await App.Current.MainPage.DisplayAlert("Submitting...", result, "OK");
    }
});

CodePudding user response:

Try with App.Current.Dispatcher.Dispatch. It works both on Android and iOS.

App.Current.Dispatcher.Dispatch(async () =>
{
     bool answer = await App.Current.MainPage.DisplayAlert("Rating", "your message", "Yes", "No");
     if (answer)
        {
             // do it
        }

});

Android

iOS

  • Related