private static Task <PopupAction> CreateAlert(UIAlertControllerStyle alertStyle, string title, string message, IEnumerable <PopupAction> actions) { var tcs = new TaskCompletionSource <PopupAction>(); var vc = UIApplication.SharedApplication.KeyWindow.RootViewController; while (vc.PresentedViewController != null) { vc = vc.PresentedViewController; } var alert = UIAlertController.Create(title, message, alertStyle); foreach (var action in actions) { if (action.IsDefault && action.IsCancel) { throw new InvalidOperationException(); } var style = action.IsCancel ? UIAlertActionStyle.Cancel : (action.IsDefault ? UIAlertActionStyle.Default : UIAlertActionStyle.Destructive); alert.AddAction(UIAlertAction.Create(action.Text, style, a => { try { action?.Command?.Execute(action.CommandParameter); tcs.TrySetResult(action); } catch (Exception e) { tcs.SetException(e); } })); } //if (alert.PopoverPresentationController != null) // alert.PopoverPresentationController.BarButtonItem = myItem; vc.PresentViewController(alert, animated: true, completionHandler: null); return(tcs.Task); }
/// <summary> /// Displays an error message /// </summary> /// <param name="title">Title, which is shown most prominently</param> /// <param name="message">Message, which is shown beneath the title</param> /// <param name="sourceView">If not null, the message is shown as a popover originating from this view</param> /// <param name="completion">Action to take after user acknowledges the error</param> private void ShowError(string title, string message, UIView sourceView = null, Action completion = null) { UIAlertControllerStyle preferredStyle = sourceView == null ? UIAlertControllerStyle.Alert : UIAlertControllerStyle.ActionSheet; // Create a new Alert Controller UIAlertController actionSheetAlert = UIAlertController.Create(title, message, preferredStyle); // Add Actions actionSheetAlert.AddAction(UIAlertAction.Create("OkAlertActionButtonText".Localize(), UIAlertActionStyle.Default, (value) => completion())); if (sourceView != null) { // Required for iPad - You must specify a source for the Action Sheet since it is displayed as a popover UIPopoverPresentationController presentationPopover = actionSheetAlert.PopoverPresentationController; if (presentationPopover != null) { presentationPopover.SourceView = sourceView; presentationPopover.PermittedArrowDirections = UIPopoverArrowDirection.Any; } } // Display the alert PresentViewController(actionSheetAlert, true, null); }
public UIAlertController([Optional] string title, [Optional] string message, UIAlertControllerStyle preferredStyle) { }
public SupportRotationAlertController(string?title, string?message, UIAlertControllerStyle controllerStyle) { Title = title; Message = message; PreferredStyle = controllerStyle; }