如何从弹出控件获取用户输入 [英] How to get user input from a popup control

查看:105
本文介绍了如何从弹出控件获取用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UserControl,它利用wp7中的弹出窗口.用户控件具有一个用于输入的文本框和一个提交按钮.我的问题是,一旦显示弹出窗口,代码就不会停止.它会继续执行代码,而不会等待用户按下Submit.

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
}

推荐答案

您可以使用事件或异步方法来完成此操作.对于该事件,您可以订阅弹出窗口的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

这篇关于如何从弹出控件获取用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆