public static async Task <string> ShowAsync(string description, string title, string defaultValue = "", string watermark = null, string toolTip = null, bool multiline = false, bool passwordMode = false, bool required = false, int maxLength = -1, IEnumerable <string> suggestions = null, CancellationToken cancellation = default(CancellationToken)) { if (passwordMode && suggestions != null) { throw new ArgumentException(@"Can’t have suggestions with password mode"); } if (passwordMode && multiline) { throw new ArgumentException(@"Can’t use multiline input area with password mode"); } if (suggestions != null && multiline) { throw new ArgumentException(@"Can’t use multiline input area with suggestions"); } var dialog = new Prompt(title, description, defaultValue, watermark, toolTip, multiline, passwordMode, required, maxLength, suggestions); try { await dialog.ShowAsync(cancellation); } catch (TaskCanceledException) { return(null); } var result = dialog.Result; if (maxLength != -1 && result?.Length > maxLength) { result = result.Substring(0, maxLength); } return(result); }
public static async Task <string> Show([Localizable(false)] string url, Regex titleMatch, string description, string title, string watermark = null, string toolTip = null, bool multiline = false, bool passwordMode = false, int maxLength = -1, IEnumerable <string> suggestions = null, Window window = null, CancellationToken cancellation = default(CancellationToken)) { if (window == null) { window = Application.Current?.Windows.OfType <Window>().SingleOrDefault(x => x.IsActive) ?? Application.Current?.MainWindow; if (window == null) { return(null); } } WindowsHelper.ViewInBrowser(url); string code = null; var waiting = true; var ready = false; EventHandler handler = async(sender, args) => { if (!waiting) { return; } waiting = false; // ReSharper disable once AccessToModifiedClosure code = await Prompt.ShowAsync(title, description, code, watermark, toolTip, multiline, passwordMode, false, maxLength, suggestions, cancellation); ready = true; }; window.Activated += handler; for (var i = 0; i < 500 && waiting; i++) { if (i > 0 && window.IsFocused) { handler(null, null); break; } if (code == null) { code = GetOpenWindows() .Select(x => titleMatch.Match(x.Value)) .FirstOrDefault(x => x.Success)? .Groups.OfType <Group>().ElementAtOrDefault(1)? .Value.Trim(); } await Task.Delay(200, cancellation); if (cancellation.IsCancellationRequested) { window.Activated -= handler; return(null); } } window.Activated -= handler; for (var i = 0; i < 500 && !waiting && !ready; i++) { await Task.Delay(200, cancellation); if (cancellation.IsCancellationRequested) { return(null); } } return(string.IsNullOrWhiteSpace(code) ? null : code.Trim()); }
public static async Task<string> ShowAsync(string description, string title, string defaultValue = "", string watermark = null, string toolTip = null, bool multiline = false, bool passwordMode = false, bool required = false, int maxLength = -1, IEnumerable<string> suggestions = null, CancellationToken cancellation = default(CancellationToken)) { if (passwordMode && suggestions != null) throw new ArgumentException(@"Can’t have suggestions with password mode"); if (passwordMode && multiline) throw new ArgumentException(@"Can’t use multiline input area with password mode"); if (suggestions != null && multiline) throw new ArgumentException(@"Can’t use multiline input area with suggestions"); var dialog = new Prompt(title, description, defaultValue, watermark, toolTip, multiline, passwordMode, required, maxLength, suggestions); try { await dialog.ShowAsync(cancellation); } catch (TaskCanceledException) { return null; } var result = dialog.Result; if (maxLength != -1 && result?.Length > maxLength) { result = result.Substring(0, maxLength); } return result; }