public override Task <int?> ShowDialogAsync( object body, IEnumerable <object> buttons, string caption = null, int defaultAcceptButtonIndex = -1, DialogController dialogController = null) { int clickedButtonIndex = -1; string[] buttonLabelArray; string cancelButtonLabel = null; if (buttons != null) { object[] tempArray = buttons.ToArray(); if (tempArray.Length > 0) { cancelButtonLabel = tempArray[0] + string.Empty; buttonLabelArray = new string[tempArray.Length - 1]; Array.Copy(tempArray, 1, buttonLabelArray, 0, tempArray.Length - 1); } else { List <string> temp = new List <string>(); foreach (var o in tempArray) { string arrayValue = o?.ToString() ?? string.Empty; temp.Add(arrayValue); } buttonLabelArray = temp.ToArray(); } } else { buttonLabelArray = new string[0]; } if (cancelButtonLabel == null) { cancelButtonLabel = DefaultOkaySymbol; } string bodyText = body?.ToString() ?? string.Empty; var stringParserService = Dependency.Resolve <IStringParserService>(); if (!string.IsNullOrWhiteSpace(bodyText)) { bodyText = stringParserService.Parse(bodyText); } var source = new TaskCompletionSource <int?>(); UIAlertView alert; if (caption == null) { alert = new UIAlertView { Message = bodyText }; int cancelButtonIndex = 0; foreach (var buttonLabel in buttonLabelArray) { alert.AddButton(buttonLabel); cancelButtonIndex++; } alert.AddButton(cancelButtonLabel); alert.CancelButtonIndex = cancelButtonIndex; } else { caption = stringParserService.Parse(caption); alert = new UIAlertView( caption, bodyText, null, cancelButtonLabel, buttonLabelArray); } alert.Clicked += (sender, buttonArgs) => { clickedButtonIndex = (int)buttonArgs.ButtonIndex; source.SetResult(clickedButtonIndex); }; alert.Canceled += delegate { source.SetResult(null); }; try { alert.Show(); } catch (Exception ex) { source.SetException(ex); } return(source.Task); }
public override Task <int?> ShowDialogAsync( object body, IEnumerable <object> buttons, string caption = null, int defaultAcceptButtonIndex = -1, DialogController dialogController = null) { Context context = ResolveContext(); AlertDialog.Builder builder = CreateAlertDialogBuilder(context, DialogStyle); if (dialogController != null && !dialogController.Cancellable) { builder.SetCancelable(false); } if (!string.IsNullOrWhiteSpace(caption)) { var stringParserService = Dependency.Resolve <IStringParserService>(); var parsedText = stringParserService.Parse(caption); builder.SetTitle(parsedText); } var bodyView = body as View; if (bodyView != null) { builder.SetView(bodyView); } else { var sequence = body as ICharSequence; if (sequence != null) { builder.SetMessage(sequence); } else { string bodyText = body?.ToString(); if (!string.IsNullOrWhiteSpace(bodyText)) { var stringParserService = Dependency.Resolve <IStringParserService>(); var parsedText = stringParserService.Parse(bodyText); ; builder.SetMessage(parsedText); } } } List <string> labels = null; int labelCount = 0; if (buttons != null) { labels = new List <string>(); foreach (var button in buttons) { string buttonText = button.ToString(); labels.Add(buttonText); } labelCount = labels.Count; } var resultSource = new TaskCompletionSource <int?>(); if (labelCount >= 2) { builder.SetNegativeButton(labels[0], (dialog, whichButton) => { resultSource.TrySetResult(0); }); for (int i = 1; i < labelCount - 1; i++) { int iClosureCopy = i; builder.SetNeutralButton(labels[i], (dialog, whichButton) => { resultSource.TrySetResult(iClosureCopy); }); } builder.SetPositiveButton(labels[labelCount - 1], (dialog, whichButton) => { int selectedIndex = labelCount - 1; resultSource.TrySetResult(selectedIndex); }); } else { if (labelCount == 1) { string buttonLabel = labels[0]; builder.SetPositiveButton(buttonLabel, (dialog, whichButton) => { resultSource.TrySetResult(0); }); } } builder.NothingSelected += (sender, e) => resultSource.TrySetResult(-1); Android.App.Application.SynchronizationContext.Post((object state) => { try { Interlocked.Increment(ref openDialogCount); AlertDialog alertDialog = builder.Show(); var dialogStyles = dialogController?.DialogStyles; if (dialogStyles.HasValue) { var styles = dialogStyles.Value; var lp = new WindowManagerLayoutParams(); Window window = alertDialog.Window; lp.CopyFrom(window.Attributes); var stretchHorizontal = (styles & DialogStyles.StretchHorizontal) == DialogStyles.StretchHorizontal; var stretchVertical = (styles & DialogStyles.StretchVertical) == DialogStyles.StretchVertical; lp.Width = stretchHorizontal ? ViewGroup.LayoutParams.MatchParent : lp.Width; lp.Height = stretchVertical ? ViewGroup.LayoutParams.MatchParent : lp.Height; //ViewGroup.LayoutParams.WrapContent; window.Attributes = lp; } //var backgroundImage = dialogController?.BackgroundImage; // //if (backgroundImage != null) //{ // //Window window = alertDialog.Window; // //window.SetBackgroundDrawable(backgroundImage);; //} alertDialog.CancelEvent += delegate { resultSource.TrySetResult(-1); }; if (dialogController != null) { dialogController.CloseRequested += delegate { if (alertDialog.IsShowing) { alertDialog.Cancel(); } }; } /* Subscribing to the DismissEvent to set the result source * is unnecessary as other events are always raised. * The DismissEvent is, however, always raised and thus * we place the bodyView removal code here. */ alertDialog.DismissEvent += (sender, args) => { Interlocked.Decrement(ref openDialogCount); builder.SetView(null); try { (bodyView?.Parent as ViewGroup)?.RemoveView(bodyView); } catch (ObjectDisposedException) { /* View was already disposed in user code. */ } catch (Exception ex) { var log = Dependency.Resolve <ILog>(); log.Debug("Exception raised when removing view from alert.", ex); } }; if (AlertDialogDividerColor.HasValue) { var resources = context.Resources; int id = resources.GetIdentifier("titleDivider", "id", "android"); View titleDivider = alertDialog.FindViewById(id); if (titleDivider != null) { var color = AlertDialogDividerColor.Value; if (color == Color.Transparent) { titleDivider.Visibility = ViewStates.Gone; } titleDivider.SetBackgroundColor(color); } } if (AlertDialogTitleColor.HasValue) { var resources = context.Resources; int id = resources.GetIdentifier("alertTitle", "id", "android"); var textView = alertDialog.FindViewById <TextView>(id); if (textView != null) { var color = AlertDialogTitleColor.Value; textView.SetTextColor(color); } } if (AlertDialogBackgroundColor.HasValue) { var v = bodyView ?? alertDialog.ListView; v.SetBackgroundColor(AlertDialogBackgroundColor.Value); } } catch (WindowManagerBadTokenException ex) { /* See http://stackoverflow.com/questions/2634991/android-1-6-android-view-windowmanagerbadtokenexception-unable-to-add-window*/ resultSource.SetException(new Exception( "Unable to use the Application.Context object to create a dialog. Please either set the Context property of this DialogService or register the current activity using Dependency.Register<Activity>(myActivity)", ex)); } }, null); return(resultSource.Task); }
// public string DefaultOkaySymbol = "Ok";// '\u2714' + ""; // public string DefaultCancelSymbol = "Cancel";// '\u2718' + ""; public override async Task <DialogResult> ShowDialogAsync( object content, string caption, DialogButton dialogButton, DialogImage dialogImage = DialogImage.None, DialogController dialogController = null) { List <string> buttons = new List <string>(); if (dialogButton == DialogButton.OK) { buttons.Add(Strings.Okay()); } else if (dialogButton == DialogButton.OKCancel) { buttons.Add(Strings.Cancel()); buttons.Add(Strings.Okay()); } else if (dialogButton == DialogButton.YesNo) { buttons.Add(Strings.No()); buttons.Add(Strings.Yes()); } else if (dialogButton == DialogButton.YesNoCancel) { buttons.Add(Strings.No()); buttons.Add(Strings.Cancel()); buttons.Add(Strings.Yes()); } var selectedIndex = await ShowDialogAsync( content, buttons, caption, 0, dialogController); if (selectedIndex < 0) { return(DialogResult.Cancel); } switch (selectedIndex) { case 0: switch (dialogButton) { case DialogButton.OK: return(DialogResult.OK); case DialogButton.OKCancel: return(DialogResult.Cancel); case DialogButton.YesNo: return(DialogResult.No); case DialogButton.YesNoCancel: return(DialogResult.No); } break; case 1: switch (dialogButton) { case DialogButton.OKCancel: return(DialogResult.OK); case DialogButton.YesNo: return(DialogResult.Yes); case DialogButton.YesNoCancel: return(DialogResult.Cancel); } break; case 2: switch (dialogButton) { case DialogButton.YesNoCancel: return(DialogResult.Yes); } break; } throw new IndexOutOfRangeException( "Index returned from dialog is out of range. " + selectedIndex); }
public override Task <DialogResult> ShowDialogAsync( object content, string caption, DialogButton dialogButton, DialogImage dialogImage = DialogImage.None, DialogController dialogController = null) { Window mainWindow = GetActiveWindow(); Dispatcher dispatcher = mainWindow != null ? mainWindow.Dispatcher : Dispatcher.CurrentDispatcher; string bodyString = content?.ToString().Parse() ?? string.Empty; string captionString = caption?.Parse() ?? string.Empty; if (dispatcher == null || dispatcher.CheckAccess()) { MessageBoxResult messageBoxResult; try { Interlocked.Increment(ref openDialogCount); if (mainWindow != null) { /* We are on the UI thread, and hence no need to invoke the call.*/ messageBoxResult = MessageBox.Show( mainWindow, bodyString, captionString, dialogButton.TranslateToMessageBoxButton(), dialogImage.TranslateToMessageBoxButton()); } else { messageBoxResult = MessageBox.Show( bodyString, captionString, dialogButton.TranslateToMessageBoxButton(), dialogImage.TranslateToMessageBoxButton()); } } finally { Interlocked.Decrement(ref openDialogCount); } return(Task.FromResult( messageBoxResult.TranslateToMessageBoxResult())); } DialogResult result = DialogResult.OK; /* Satisfy compiler with default value. */ dispatcher.Invoke((ThreadStart) delegate { MessageBoxResult messageBoxResult; try { Interlocked.Increment(ref openDialogCount); if (mainWindow != null) { /* We are on the UI thread, * and hence no need to invoke the call.*/ messageBoxResult = MessageBox.Show(mainWindow, bodyString, captionString, dialogButton.TranslateToMessageBoxButton(), dialogImage.TranslateToMessageBoxButton()); } else { messageBoxResult = MessageBox.Show(bodyString, captionString, dialogButton.TranslateToMessageBoxButton(), dialogImage.TranslateToMessageBoxButton()); } } finally { Interlocked.Decrement(ref openDialogCount); } result = messageBoxResult.TranslateToMessageBoxResult(); }); return(Task.FromResult(result)); }