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

标签 c# xamarin.forms console.readline

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

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

这是我现在的控制台类:

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!");
        }

但是输出是这样的: output

我尝试了 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事件(按下 Enter 键时触发)。

关于c# - (C#) (Xamarin.Forms) 自定义 ReadLine(),暂停用户响应代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46488977/

相关文章:

c# - Azure 连接失败,因为连接的主机无法响应 ip :443

c# - 在 word 中的现有选项卡中自定义新菜单

xamarin.forms - Xamarin 选择器未绑定(bind)

c# - 将 JSON 反序列化到 SQLite 数据库中

c# - 如何从键盘读取 "Enter"退出程序

c# - 如何强制使用扩展方法而不是带有参数的实例方法?

c# - Xamarin.Forms 的 ECDsa

xamarin - 制作一个 2 单元格的水平堆栈布局,占设备屏幕宽度的 50%

java - 未找到控制台。如何获取 JVM 的控制台?

c# - Console.ReadLine() 不在 C# 中保持控制台打开