且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从弹出控件获取用户输入

更新时间:2023-10-19 20:41:10

您可以使用事件或异步方法来完成此操作.对于该事件,您可以订阅弹出窗口的Closed事件.

You could accomplish this with either an event, or an async method. For the event you would subscribe to a Closed event of your popup.

    InputBox.Closed += OnInputClosed;
    InputBox.Show("New Highscore!", "Enter your name!", "Submit");

...

private void OnInputClosed(object sender, EventArgs e)
{
    string name = InputBox.Name;
}

您将在用户按下确定"按钮时触发该事件

You would fire the event when the user pushes the OK button

private void OnOkayButtonClick(object sender, RoutedEventArgs routedEventArgs)
{
    Closed(this, EventArgs.Empty);
}

另一个选择是使用异步方法.为此,您需要异步Nuget包.若要使方法异步,请使用两个主要对象,一个任务 TaskCompletionSource .

The other option is to use an async method. For this you need the async Nuget pack. To make a method async you use two main objects, a Task and a TaskCompletionSource.

private Task<string> Show(string one, string two, string three)
{
    var completion = new TaskCompletionSource<string>();

    OkButton.Click += (s, e) =>
        {
            completion.SetResult(NameTextBox.Text);
        };


    return completion.Task;
}

然后您将等待对show方法的调用.

You would then await the call to the show method.

string user = await InputBox.Show("New Highscore!", "Enter your name!", "Submit");

我相信 Coding4Fun工具包也具有一些不错的

I believe the Coding4Fun toolkit also has some nice input boxes