Search code examples
iosalertmaui

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


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?


Solution

  • You could write Platform code to make it. I make a small demo. You could have a try.

    In Project folder, create a new partial class, let's call it DisplayAlertService:

    namespace CommunityPopup75242427
    {
        public partial class DisplayAlertService
        {
            public partial void DisplayAlert();
        }
    }
    

    Then creat another partial class in Platforms/iOS folder, called it DisplayAlertService also. Pay attention to the namespace should be the same as above file:

    namespace CommunityPopup75242427
    {
        public partial class DisplayAlertService
        {
            public partial void DisplayAlert()
            {
    
                var okCancelAlertController = UIAlertController.Create("Alert Title", "Choose from two buttons", UIAlertControllerStyle.Alert);
    
                //Add Actions
                okCancelAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, alert => Console.WriteLine("Okay was clicked")));             
                okCancelAlertController.AddAction(UIAlertAction.Create("Cancel", UIAlertActionStyle.Cancel, alert => Console.WriteLine("Cancel was clicked")));
                UIWindow window = UIApplication.SharedApplication.Delegate.GetWindow();window.RootViewController.PresentedViewController.PresentViewController(okCancelAlertController, true, null);
            }
        }
    }
    

    Then in popup, you could invoke it using a button clicked for example:

    async void mybutton_Clicked(System.Object sender, System.EventArgs e)
    {
        Device.BeginInvokeOnMainThread(() =>
        {
            var d = new DisplayAlertService();
            d.DisplayAlert();
        });
    }
    

    For more info, you could refer to:

    1.How To Write Platform-Specific Code in .NET MAUI and MauiPlatformCodeSample code.

    2.Displaying Alerts in Xamarin.iOS.

    Hope it works for you.