From 6ee5c18011f89ea73fa385d50968116f2b941a06 Mon Sep 17 00:00:00 2001 From: Gerald Versluis Date: Wed, 6 Nov 2019 15:48:39 +0100 Subject: [PATCH] [C] Added ability to predefine value to prompt (#8362) fixes #8346 * Added ability to predefine value * Prevent ABI break * Update Issue6713.cs --- .../Xamarin.Forms.Controls.Issues.Shared/Issue6713.cs | 16 ++++++++++++++-- Xamarin.Forms.Core/Page.cs | 9 ++++++++- Xamarin.Forms.Core/PromptArguments.cs | 12 +++++++++++- Xamarin.Forms.Platform.Android/PopupManager.cs | 2 +- Xamarin.Forms.Platform.iOS/Platform.cs | 1 + 5 files changed, 35 insertions(+), 5 deletions(-) diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue6713.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue6713.cs index 6643c56..21a9f02 100644 --- a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue6713.cs +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue6713.cs @@ -7,6 +7,8 @@ namespace Xamarin.Forms.Controls.Issues [Issue(IssueTracker.Github, 6713, "[Enhancement] Display prompts", PlatformAffected.iOS | PlatformAffected.Android)] public class Issue6713 : TestContentPage // or TestMasterDetailPage, etc ... { + const string PrefilledValue = "1337"; + protected override void Init() { var scrollView = new ScrollView(); @@ -22,7 +24,7 @@ namespace Xamarin.Forms.Controls.Issues var button = new Button { Text = "Default keyboard" }; button.Clicked += async (sender, e) => { - var result = await DisplayPromptAsync("What’s the most useless product around today?", "The USB pet rock is definitely up there. What items do you have a hard time believing they actually exist?"); + var result = await DisplayPromptAsync("What’s the most useless product around today?", "The USB pet rock is definitely up there. What items do you have a hard time believing they actually exist?", initialValue: ""); if (result != null) (sender as Button).Text = result; @@ -32,13 +34,23 @@ namespace Xamarin.Forms.Controls.Issues var button2 = new Button { Text = "Numeric keyboard" }; button2.Clicked += async (sender, e) => { - var result = await DisplayPromptAsync("What’s the meaning of life?", "You know that number.", maxLength:2, keyboard:Keyboard.Numeric); + var result = await DisplayPromptAsync("What’s the meaning of life?", "You know that number.", maxLength:2, keyboard:Keyboard.Numeric, initialValue: ""); if (result != null) (sender as Button).Text = result; }; stackLayout.Children.Add(button2); + var button3 = new Button { Text = "Prefilled" }; + button3.Clicked += async (sender, e) => + { + var result = await DisplayPromptAsync("The input field should have a value already", $"And it should be {PrefilledValue}", initialValue: PrefilledValue); + + if (result != null) + (sender as Button).Text = result; + }; + stackLayout.Children.Add(button3); + scrollView.Content = stackLayout; Content = scrollView; } diff --git a/Xamarin.Forms.Core/Page.cs b/Xamarin.Forms.Core/Page.cs index 07efb60..22cfee0 100644 --- a/Xamarin.Forms.Core/Page.cs +++ b/Xamarin.Forms.Core/Page.cs @@ -209,9 +209,16 @@ namespace Xamarin.Forms return args.Result.Task; } + [Obsolete("DisplayPromptAsync overload is obsolete as of version 4.5.0 and is no longer supported.")] + [EditorBrowsable(EditorBrowsableState.Never)] public Task DisplayPromptAsync(string title, string message, string accept = "OK", string cancel = "Cancel", string placeholder = null, int maxLength = -1, Keyboard keyboard = default(Keyboard)) { - var args = new PromptArguments(title, message, accept, cancel, placeholder, maxLength, keyboard); + return DisplayPromptAsync(title, message, accept, cancel, placeholder, maxLength, keyboard, ""); + } + + public Task DisplayPromptAsync(string title, string message, string accept = "OK", string cancel = "Cancel", string placeholder = null, int maxLength = -1, Keyboard keyboard = default(Keyboard), string initialValue = "") + { + var args = new PromptArguments(title, message, accept, cancel, placeholder, maxLength, keyboard, initialValue); MessagingCenter.Send(this, PromptSignalName, args); return args.Result.Task; } diff --git a/Xamarin.Forms.Core/PromptArguments.cs b/Xamarin.Forms.Core/PromptArguments.cs index 5170119..0cb1a24 100644 --- a/Xamarin.Forms.Core/PromptArguments.cs +++ b/Xamarin.Forms.Core/PromptArguments.cs @@ -1,4 +1,5 @@ -using System.ComponentModel; +using System; +using System.ComponentModel; using System.Threading.Tasks; namespace Xamarin.Forms.Internals @@ -6,13 +7,20 @@ namespace Xamarin.Forms.Internals [EditorBrowsable(EditorBrowsableState.Never)] public class PromptArguments { + [Obsolete("PromptArguments overload is obsolete as of version 4.5.0 and is no longer supported.")] + [EditorBrowsable(EditorBrowsableState.Never)] public PromptArguments(string title, string message, string accept = "OK", string cancel = "Cancel", string placeholder = null, int maxLength = -1, Keyboard keyboard = default(Keyboard)) + : this (title, message, accept, cancel, placeholder, maxLength, keyboard, "") + { } + + public PromptArguments(string title, string message, string accept = "OK", string cancel = "Cancel", string placeholder = null, int maxLength = -1, Keyboard keyboard = default(Keyboard), string initialValue = "") { Title = title; Message = message; Accept = accept; Cancel = cancel; Placeholder = placeholder; + InitialValue = initialValue; MaxLength = maxLength; Keyboard = keyboard ?? Keyboard.Default; Result = new TaskCompletionSource(); @@ -28,6 +36,8 @@ namespace Xamarin.Forms.Internals public string Placeholder { get; } + public string InitialValue { get; } + public int MaxLength { get; } public Keyboard Keyboard { get; } diff --git a/Xamarin.Forms.Platform.Android/PopupManager.cs b/Xamarin.Forms.Platform.Android/PopupManager.cs index 91dc9b1..2c2c8fd 100644 --- a/Xamarin.Forms.Platform.Android/PopupManager.cs +++ b/Xamarin.Forms.Platform.Android/PopupManager.cs @@ -141,7 +141,7 @@ namespace Xamarin.Forms.Platform.Android alertDialog.SetMessage(arguments.Message); var frameLayout = new FrameLayout(Activity); - var editText = new EditText(Activity) { Hint = arguments.Placeholder }; + var editText = new EditText(Activity) { Hint = arguments.Placeholder, Text = arguments.InitialValue }; var layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent) { LeftMargin = (int)(22 * Activity.Resources.DisplayMetrics.Density), diff --git a/Xamarin.Forms.Platform.iOS/Platform.cs b/Xamarin.Forms.Platform.iOS/Platform.cs index ef0c9f9..369e9f6 100644 --- a/Xamarin.Forms.Platform.iOS/Platform.cs +++ b/Xamarin.Forms.Platform.iOS/Platform.cs @@ -366,6 +366,7 @@ namespace Xamarin.Forms.Platform.iOS alert.AddTextField(uiTextField => { uiTextField.Placeholder = arguments.Placeholder; + uiTextField.Text = arguments.InitialValue; uiTextField.ShouldChangeCharacters = (field, range, replacementString) => arguments.MaxLength <= -1 || field.Text.Length + replacementString.Length - range.Length <= arguments.MaxLength; uiTextField.ApplyKeyboard(arguments.Keyboard); }); -- 2.7.4