Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIAlertView Button Event

I have a question on Xamarin.Forms, I'm trying to navigate back page from detail page when UIAlertView pressed OK. But I couldn't do it. Here is my code;

{
    UIAlertView alert = new UIAlertView();
    alert.Title = "Warning";
    alert.AddButton("OK");
    alert.Message = "Payment not found!";
    alert.Clicked += ButtonClicked;
    alert.Show();
    return paymentList;
 }

 private void ButtonClicked(object sender, UIButtonEventArgs e)
 {
     //???
 }

Thanks.

like image 822
ozmert75 Avatar asked Jun 10 '26 08:06

ozmert75


2 Answers

First of all, Xamarin.Forms doesn't have UIAlertView class (Inside the PCL project). You can display alert there by calling asynchronous DisplayAlert method inside the Page instance. For example:

public class PaymentPage
{
    ...

    async void OnNoPaymentMethodDetected(object sender, EventArgs e)
    {
        await DisplayAlert("Error", "You have no payment method registered", "Okay");
        await Navigation.PopAsync(true);
    }
    ...
}

Note: your Pop Navigation method depends on your Push Navigation method. For instance, if you've used PushModalAsync(page2), then you have to use PopModalAsync() in page2 to move back, otherwise PopAsync() will cause an error.

If you want to have similar alert in iOS-specific project, you have to use the next code:

var controller = UIAlertController.Create("Error", "No payment method detected", UIAlertControllerStyle.Alert);

controller.AddAction(UIAlertAction.Create("Okay", UIAlertActionStyle.Cancel, (sender) =>
{
    NavigationController.PopViewController(true);
}));

ShowDetailViewController(controller, null);
like image 82
Taras Shevchuk Avatar answered Jun 12 '26 21:06

Taras Shevchuk


UIAleartView does not have specific events for Buttons. But, you can access the index of the button pressed from the UIButtonEventArgs (ex: e.ButtonIndex) verify what button is pressed

like image 29
Prashant Cholachagudda Avatar answered Jun 12 '26 22:06

Prashant Cholachagudda



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!