Пример #1
0
        protected virtual void SetDefaultPrompt(ContentDialog dialog, StackPanel stack, PromptConfig config)
        {
            var txt = new TextBox
            {
                PlaceholderText = config.Placeholder ?? String.Empty,
                Text            = config.Text ?? String.Empty
            };

            if (config.MaxLength != null)
            {
                txt.MaxLength = config.MaxLength.Value;
            }

            stack.Children.Add(txt);

            txt.SelectAll();

            dialog.PrimaryButtonCommand = new Command(() =>
            {
                config.OnAction?.Invoke(new PromptResult(true, txt.Text.Trim()));
                dialog.Hide();
            });

            if (config.OnTextChanged == null)
            {
                return;
            }

            var args = new PromptTextChangedArgs {
                Value = txt.Text
            };

            config.OnTextChanged(args);
            dialog.IsPrimaryButtonEnabled = args.IsValid;

            txt.TextChanged += (sender, e) =>
            {
                args.IsValid = true; // reset
                args.Value   = txt.Text;
                config.OnTextChanged(args);
                dialog.IsPrimaryButtonEnabled = args.IsValid;

                if (!args.Value.Equals(txt.Text))
                {
                    txt.Text            = args.Value;
                    txt.SelectionStart  = Math.Max(0, txt.Text.Length);
                    txt.SelectionLength = 0;
                }
            };
        }
Пример #2
0
        public override IDisposable Prompt(PromptConfig config)
        {
            return(this.Present(() =>
            {
                var dlg = UIAlertController.Create(config.Title ?? String.Empty, config.Message, UIAlertControllerStyle.Alert);
                UITextField txt = null;

                if (config.IsCancellable)
                {
                    dlg.AddAction(UIAlertAction.Create(config.CancelText, UIAlertActionStyle.Cancel, x =>
                                                       config.OnAction?.Invoke(new PromptResult(false, txt.Text.Trim())
                                                                               )));
                }

                var btnOk = UIAlertAction.Create(config.OkText, UIAlertActionStyle.Default, x =>
                                                 config.OnAction?.Invoke(new PromptResult(true, txt.Text.Trim())
                                                                         ));
                dlg.AddAction(btnOk);
                dlg.AddTextField(x =>
                {
                    txt = x;
                    if (config.MaxLength != null)
                    {
                        txt.ShouldChangeCharacters = (field, replacePosition, replacement) =>
                        {
                            var updatedText = new StringBuilder(field.Text);
                            updatedText.Remove((int)replacePosition.Location, (int)replacePosition.Length);
                            updatedText.Insert((int)replacePosition.Location, replacement);
                            return updatedText.ToString().Length <= config.MaxLength.Value;
                        };
                    }

                    if (config.OnTextChanged != null)
                    {
                        txt.Ended += (sender, e) =>
                        {
                            var args = new PromptTextChangedArgs {
                                Value = txt.Text
                            };
                            config.OnTextChanged(args);
                            btnOk.Enabled = args.IsValid;
                            if (!txt.Text.Equals(args.Value))
                            {
                                txt.Text = args.Value;
                            }
                        };
                    }
                    this.SetInputType(txt, config.InputType);
                    txt.Placeholder = config.Placeholder ?? String.Empty;
                    if (config.Text != null)
                    {
                        txt.Text = config.Text;
                    }
                });
                return dlg;
            }));
        }
Пример #3
0
        static void ValidatePrompt(UITextField txt, UIAlertAction btn, PromptConfig config)
        {
            var args = new PromptTextChangedArgs {
                Value = txt.Text
            };

            config.OnTextChanged(args);
            btn.Enabled = args.IsValid;
            if (!txt.Text.Equals(args.Value))
            {
                txt.Text = args.Value;
            }
        }
Пример #4
0
        public override IDisposable Prompt(PromptConfig config)
        {
            Dispatch(() =>
            {
                var dialog = new FormsContentDialog()
                {
                    DataContext = config,
                    Title       = config.Title,
                    // Content will be set
                    IsPrimaryButtonEnabled   = true,
                    PrimaryButtonText        = config.OkText,
                    IsSecondaryButtonEnabled = config.IsCancellable,
                    SecondaryButtonText      = config.CancelText
                };

                if (config.InputType == InputType.Password || config.InputType == InputType.NumericPassword)
                {
                    var control = new PasswordPromptControl();
                    control.PasswordEdit.PasswordChanged += (s, e) =>
                    {
                        config.Text = control.PasswordEdit.Password;
                        if (config.OnTextChanged != null)
                        {
                            var args = new PromptTextChangedArgs()
                            {
                                Value = control.PasswordEdit.Password
                            };
                            config.OnTextChanged(args);
                            dialog.IsPrimaryButtonEnabled = args.IsValid;
                            if (control.PasswordEdit.Password != args.Value)
                            {
                                control.PasswordEdit.Password = args.Value;
                            }
                        }
                    };
                    dialog.Content = control;
                    // First run of text changed
                    if (config.OnTextChanged != null)
                    {
                        var args = new PromptTextChangedArgs()
                        {
                            Value = control.PasswordEdit.Password
                        };
                        config.OnTextChanged(args);
                        dialog.IsPrimaryButtonEnabled = args.IsValid;
                        control.PasswordEdit.Password = args.Value;
                    }
                }
                else
                {
                    var control = new DefaultPromptControl();
                    control.TextEdit.TextChanged += (s, e) =>
                    {
                        if (config.OnTextChanged != null)
                        {
                            var args = new PromptTextChangedArgs()
                            {
                                Value = control.TextEdit.Text
                            };
                            config.OnTextChanged(args);
                            dialog.IsPrimaryButtonEnabled = args.IsValid;
                            if (control.TextEdit.Text != args.Value)
                            {
                                int selStart                    = control.TextEdit.SelectionStart;
                                control.TextEdit.Text           = args.Value;
                                control.TextEdit.SelectionStart = selStart;
                            }
                        }
                    };
                    dialog.Content = control;
                    // First run of text changed
                    if (config.OnTextChanged != null)
                    {
                        var args = new PromptTextChangedArgs()
                        {
                            Value = control.TextEdit.Text
                        };
                        config.OnTextChanged(args);
                        dialog.IsPrimaryButtonEnabled = args.IsValid;
                        int selStart                    = control.TextEdit.SelectionStart;
                        control.TextEdit.Text           = args.Value;
                        control.TextEdit.SelectionStart = selStart;
                    }
                }

                dialog.PrimaryButtonClick   += (s, e) => { HideContentDialog(); config.OnAction(new PromptResult(true, config.Text)); e.Cancel = true; };
                dialog.SecondaryButtonClick += (s, e) => { HideContentDialog(); config.OnAction(new PromptResult(false, config.Text)); e.Cancel = true; };
                ShowContentDialog(dialog);
            });
            return(new DisposableAction(HideContentDialog));
        }
        public override IDisposable Prompt(PromptConfig config)
        {
            var positive = new XButton
            {
                Text = config.OkText
            };
            var negative = new XButton
            {
                Text = config.CancelText
            };
            var txt = new Entry()
            {
                Placeholder = config.Placeholder ?? String.Empty,
                Text        = config.Text ?? String.Empty,
            };
            var layout = new StackLayout
            {
                Children =
                {
                    txt,
                },
                Padding = 30
            };
            var dialog = new Dialog()
            {
                Title            = config.Title ?? String.Empty,
                Subtitle         = config.Message,
                Content          = layout,
                HorizontalOption = LayoutOptions.Center,
                VerticalOption   = LayoutOptions.Center,
            };

            if (config.IsCancellable)
            {
                dialog.Negative = negative;
            }
            if (config.OkText != null)
            {
                positive.Text   = config.OkText;
                dialog.Positive = positive;
            }
            this.SetInputType(txt, config.InputType);
            if (config.MaxLength != null)
            {
                txt.TextChanged += (s, e) =>
                {
                    var entry = (Entry)s;

                    if (entry.Text.Length > config.MaxLength)
                    {
                        string entryText = entry.Text;
                        entryText  = entryText.Remove(entryText.Length - 1);
                        entry.Text = entryText;
                    }
                };
            }
            if (config.OnTextChanged != null)
            {
                if (config.InputType == InputType.Password)
                {
                    txt.IsPassword   = true;
                    txt.TextChanged += (sender, e) =>
                    {
                        txt.IsPassword = true;
                    };
                }

                var args = new PromptTextChangedArgs {
                    Value = txt.Text
                };
                config.OnTextChanged(args);
                positive.IsEnabled = args.IsValid;
                txt.TextChanged   += (s, e) =>
                {
                    args.IsValid = true;
                    args.Value   = txt.Text;
                    config.OnTextChanged(args);
                    positive.IsEnabled = args.IsValid;
                    if (!args.Value.Equals(txt.Text))
                    {
                        txt.Text = args.Value;
                        //txt.SelectionStart = Math.Max(0, txt.Text.Length);
                        //txt.SelectionLength = 0;
                    }
                };
            }
            dialog.OutsideClicked += (s, e) =>
            {
                dialog.Hide();
            };
            positive.Clicked += (s, e) =>
            {
                dialog.Hide();
                config.OnAction?.Invoke(new PromptResult(true, txt.Text.Trim()));
            };
            negative.Clicked += (s, e) =>
            {
                dialog.Hide();
                config.OnAction?.Invoke(new PromptResult(false, txt.Text));
            };
            return(Show(dialog));
        }