コード例 #1
0
        public Task <string> ShowEntryAlert(string title, string subtitle, string okText, string cancelText = null, string placeholder = null, bool isPassword = false, bool isDestructive = false, GalleyFont font = null, string initialText = null)
        {
            var tcs   = new TaskCompletionSource <string>();
            var alert = UIAlertController.Create(title, subtitle, UIAlertControllerStyle.Alert);

            cancelText.NotNull().Then(() => alert.AddAction(UIAlertAction.Create(cancelText, UIAlertActionStyle.Cancel, t => tcs.SetResult(null))));
            alert.AddAction(UIAlertAction.Create(okText, isDestructive ? UIAlertActionStyle.Destructive : UIAlertActionStyle.Default, t => tcs.SetResult(alert.TextFields.FirstOrDefault()?.Text)));
            alert.AddTextField(f =>
            {
                f.SecureTextEntry = isPassword;
                f.Text            = initialText;
                font.NotNull().Then(() => {
                    if (font.Name != null && font.Size > 0)
                    {
                        f.Font = UIFont.FromName(font.Name, (nfloat)font.Size);
                    }
                    if (font.Color != null)
                    {
                        f.TextColor = Color.FromHex(font.Color).ToUIColor();
                    }
                });
            });

            UIApplication.SharedApplication?.KeyWindow?.RootViewController?.PresentViewController(alert, true, null);
            return(tcs.Task);
        }
コード例 #2
0
 public Task <string> ShowEntryAlert(string title, string subtitle, string okText, string cancelText = null, string placeholder = null, bool isPassword = false, bool isDestructive = false, GalleyFont font = null, string initialText = null)
 => DependencyService.Get <IDialogService>().ShowEntryAlert(title, subtitle, okText, cancelText, placeholder, isPassword, isDestructive, font, initialText);
コード例 #3
0
        public Task <string> ShowEntryAlert(string title, string subtitle, string okText, string cancelText = null, string placeholder = null, bool isPassword = false, bool isDestructive = false, GalleyFont font = null, string initialText = null)
        {
            var tcs          = new TaskCompletionSource <string>();
            var alertBuilder = new AlertDialog.Builder(Forms.Context);
            var editText     = new EditText(Forms.Context)
            {
                Hint = placeholder,
                Text = initialText
            };

            isPassword.Then(() => editText.InputType = Android.Text.InputTypes.TextVariationPassword);

            font.NotNull().Then(() =>
            {
                if (font.Name != null)
                {
                    editText.SetTypeface(Typeface.CreateFromAsset(Android.App.Application.Context.Assets, font.Name), TypefaceStyle.Normal);
                }
                if (font.Size > 0)
                {
                    editText.SetTextSize(ComplexUnitType.Sp, (float)font.Size);
                }
                if (font.Color != null)
                {
                    var color = Xamarin.Forms.Color.FromHex(font.Color).ToAndroid();
                    editText.SetTextColor(color);
                    editText.Background.Mutate().SetColorFilter(color, PorterDuff.Mode.SrcAtop);
                }
            });

            alertBuilder.SetTitle(title);
            alertBuilder.SetMessage(subtitle);


            cancelText.NotNull().Then(() => alertBuilder.SetNegativeButton(cancelText, (sender, e) => tcs.SetResult(null)));
            alertBuilder.SetPositiveButton(okText, (sender, e) => tcs.SetResult(editText.Text));

            var alert = alertBuilder.Create();

            alertBuilder.Dispose();
            alert.SetCanceledOnTouchOutside(true);
            alert.CancelEvent += (o, e) => tcs.SetResult(null);
            alert.SetView(editText, 15, 5, 15, 5);

            alert.Window.SetSoftInputMode(Android.Views.SoftInput.StateVisible);

            alert.Show();

            return(tcs.Task);
        }