我正在尝试在 custom 中创建自定义ReadLine()
方法Console class
。
我想Console
在. Android
_iOS
Xamarin.Forms
这是我现在的控制台类:
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!");
}
我在等待用户响应时尝试了接受的答案,但这不适用于我的项目。
现在我的问题是:如何在每个问题之后暂停代码以等待用户的响应?
提前致谢!