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.
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 usePopModalAsync()in page2 to move back, otherwisePopAsync()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);
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With