(C#) (Xamarin.Forms) 自定义 ReadLine(),用户响应的暂停代码

(C#) (Xamarin.Forms) Custom ReadLine(), pause code for user response

我正在尝试在自定义 Console class 中创建自定义 ReadLine() 方法。

我想使用 Xamarin.FormsAndroidiOS 上创建一个 Console 应用程序。

这是我现在的控制台 class:

public class DHConsole
{
    protected static Entry inEntry;
    protected static Label outLabel;
    protected static bool write = false;
    public static bool completed = false;

    /// <summary>
    /// Gets the in output.
    /// </summary>
    /// <param name="edit">Edit.</param>
    /// <param name="etr">Etr.</param>
    public static void GetInOutputX(Label oLabel, Entry iEntry)
    {
        outLabel = oLabel; // Xamarin.Forms.Label
        inEntry = iEntry;  // Xamarin.Froms.Entry
    }

    /// <summary>
    /// Write the specified output.
    /// </summary>
    /// <returns>The write.</returns>
    /// <param name="output">Output.</param>
    public static void Write(string output)
    {
        outLabel.Text += output;
        write = true;
    }

    /// <summary>
    /// Writes the line.
    /// </summary>
    /// <param name="output">Output.</param>
    public static void WriteLine(string output)
    {
        // Check if there already is set the method Write().
        if (!write)
        {
            // Set the output on a new line.
            outLabel.Text += "\n";
        }
        else
        {
           // Set the output on the current line.
           // And set write to false.
            write = false;
        }

        outLabel.Text += output;
    }

    /// <summary>
    /// Reads the line.
    /// </summary>
    /// <returns>The line.</returns>
    public static string ReadLine()
    {
        //GetLine(inEntry.Text).Wait();

        //completed = false;
        outLabel.Text += inEntry.Text;

        return inEntry.Text;
    }

    /// <summary>
    /// Reads the key.
    /// </summary>
    /// <returns>The key.</returns>
    public static string ReadKey()
    {
        string input = inEntry.Text;
        return input[input.Length - 1].ToString();
    }

    //protected async static Task GetLine(string entryText)
    //{
    //    Task<string> textTask = entryText;
    //    string text = await textTask;
    //}
}

我的想法是像这样在屏幕上获取 Console.WriteLine() 和自定义 Console.ReadLine()

        DHConsole.WriteLine("What is the firstname of this new User?");
        string firstname = DHConsole.ReadLine();

        DHConsole.WriteLine("What is the lastname of this new User?");
        string lastname = DHConsole.ReadLine();

        DHConsole.WriteLine("What is the username of this new User?");
        string username = DHConsole.ReadLine();

        DHConsole.WriteLine("What is the name of the street of this new User?");
        string street = DHConsole.ReadLine();

        DHConsole.WriteLine("What is the house number of this new User?");
        string houseNumber = DHConsole.ReadLine();

        DHConsole.WriteLine("What is the name of the city of this new User?");
        string city = DHConsole.ReadLine();

        DHConsole.WriteLine("What is the name of the country of this new User?");
        string country = DHConsole.ReadLine();

        DHConsole.WriteLine("Do you want to continue registering?[Yes/No]");
        string sContinue = DHConsole.ReadLine();

        if (IsEqualCI(sContinue, "Yes"))
        {
            users.Add(new User
            {
                Id = createId(users),
                FirstName = firstname,
                LastName = lastname,
                UserName = username,
                Street = street,
                HouseNumber = houseNumber,
                City = city,
                Country = country
            });
        }
        else
        {
            DHConsole.WriteLine("OK, bye!");
        }

但是输出是这样的:

我在 waiting for users response 上尝试了接受的答案,但这对我的项目不起作用。

现在我的问题是:如何在每个问题后暂停代码,以等待用户的响应?

提前致谢!

@Nkosi 的意思是将您的 ReadLine 修改为以下内容:

TaskCompletionSource<string> _tcs;
public Task<string> Readline()
{
    _tcs = new TaskCompletionSource<string>();

    EventHandler handler = null;
    handler = new EventHandler((sender, args) =>
    {
        var entry = sender as Entry;
        _tcs.SetResult(entry.Text);
        entry.Text = string.Empty;
        entry.Completed -= handler;
    });

    var ctrl = inEntry;
    ctrl.Completed += handler;
    ctrl.Focus();
    return _tcs.Task;
}

示例用法:

string firstname = await DHConsole.ReadLine();

基本上这段代码使用 TaskCompletionSourceCompleted 事件(按下回车键时触发)。