0

I have a UserControl that utilizes a popup window in wp7. The user control has a text box for input, and a submit button. My issue is that the code does not halt once the popup is shown. It continues on through the code and does not wait for the user to press submit.

What is a good practice for making the code "halt" similar to a message box with an "Okay" button?

//my custom popup control
InputBox.Show("New Highscore!", "Enter your name!", "Submit");
string name = InputBox.GetInput();
//it does not wait for the user to input any data at this point, and continues to the next piece of code

if (name != "")
{
     //some code
}
4

1 回答 1

1

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

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

...

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

当用户按下 OK 按钮时,您将触发该事件

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

另一种选择是使用异步方法。为此,您需要异步 Nuget 包。要使方法异步,请使用两个主要对象,一个Task和一个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 方法的调用。

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

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

于 2013-07-22T18:57:57.020 回答