コード例 #1
0
        public static async void OpenInputDialog(string title, Action <string> callback, int max_length = 0, object[] args = null)
        {
            if (PlatformFunctions.IsDialogOpen)
            {
                return;
            }

            PlatformFunctions.IsDialogOpen = true;
            string result = "";

            Engine.Pause();
#if ANDROID
            var builder = new Android.App.AlertDialog.Builder(NeonPartyGamesControllerGame.AndroidContext);
            var input   = new Android.Widget.EditText(NeonPartyGamesControllerGame.AndroidContext);
            var tcs     = new System.Threading.Tasks.TaskCompletionSource <bool>();

            builder.SetTitle(title);
            if (args != null && args.Length > 0)
            {
                input.InputType = (Android.Text.InputTypes)args[0];
            }
            else
            {
                input.InputType = Android.Text.InputTypes.ClassText;
            }
            if (max_length > 0)
            {
                input.SetFilters(new Android.Text.IInputFilter[] { new Android.Text.InputFilterLengthFilter(max_length) });
            }
            builder.SetView(input);
            builder.SetPositiveButton("OK", (sender_alert, sender_args) => {
                VibrationHelper.Vibrate();
                result = input.Text;
            });
            builder.SetOnDismissListener(new OnDismissListener(() => tcs.TrySetResult(true)));
            builder.Show();
            await tcs.Task;
#elif IOS
            var tcs = new System.Threading.Tasks.TaskCompletionSource <bool>();
            UIKit.UIAlertView alert = new UIKit.UIAlertView();
            alert.Title = title;
            alert.AddButton("OK");
            alert.AlertViewStyle = UIKit.UIAlertViewStyle.PlainTextInput;
            alert.Clicked       += (object s, UIKit.UIButtonEventArgs ev) => {
                if (ev.ButtonIndex == 0)
                {
                    result = alert.GetTextField(0).Text;
                }
            };
            alert.Dismissed += (object s, UIKit.UIButtonEventArgs ev) => {
                tcs.TrySetResult(true);
            };
            alert.Show();
            await tcs.Task;
#elif NETFX_CORE
            await Xamarin.Essentials.MainThread.InvokeOnMainThreadAsync(async() =>
            {
                var input_text_box = new Windows.UI.Xaml.Controls.TextBox
                {
                    AcceptsReturn = false,
                    Height        = 32
                };
                if (max_length > 0)
                {
                    input_text_box.MaxLength = max_length;
                }
                var dialog = new Windows.UI.Xaml.Controls.ContentDialog
                {
                    Content                  = input_text_box,
                    Title                    = title,
                    PrimaryButtonText        = "Ok",
                    IsSecondaryButtonEnabled = true,
                    SecondaryButtonText      = "Cancel",
                    DefaultButton            = Windows.UI.Xaml.Controls.ContentDialogButton.Primary
                };
                if (await dialog.ShowAsync() == Windows.UI.Xaml.Controls.ContentDialogResult.Primary)
                {
                    result = input_text_box.Text;
                }
                else
                {
                    result = "";
                }
            });
#else
        #if DEBUG
            await System.Threading.Tasks.Task.Run(() => {
                var debugger = Engine.GetFirstInstanceByType <DebuggerWithTerminal>();
                debugger?.OpenConsoleWithCustomEvaluator(value => result = value);
                while (debugger != null && debugger.ConsoleOpen)
                {
                    System.Threading.Thread.Sleep(1);
                }
            });
        #endif
#endif
            Engine.Resume();
            PlatformFunctions.IsDialogOpen = false;

            if (!string.IsNullOrWhiteSpace(result))
            {
                callback?.Invoke(result);
            }
        }