(C#)(Xamarin.Forms)Custom ReadLine(),暂停代码以供用户响应
我正在尝试在自定义的(C#)(Xamarin.Forms)Custom ReadLine(),暂停代码以供用户响应,c#,xamarin.forms,console.readline,C#,Xamarin.forms,Console.readline,我正在尝试在自定义的控制台类中创建一个自定义的ReadLine()方法 我想使用Xamarin.Forms在Android和iOS上创建一个Console应用程序 这是我现在的控制台类: public class DHConsole { protected static Entry inEntry; protected static Label outLabel; protected static bool write = false; public static
控制台类中创建一个自定义的ReadLine()
方法
我想使用Xamarin.Forms
在Android
和iOS
上创建一个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;
//}
}
但输出结果如下所示:
我尝试了公认的答案,但这对我的项目不起作用
现在我的问题是:如何在每个问题之后暂停代码,以等待用户的响应
提前谢谢 @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;
}
基本上,此代码使用TaskCompletionSource
和事件(按下enter键时触发)。如果条目有文本更改事件,则订阅该事件并侦听新行字符(enter)从那里使用任务完成源等待输入并让任务完成以允许代码流动我尝试使用async
wait
任务,正如您在我的问题中的DHConsole
类中看到的那样。这就是你的意思吗@Nkosi也许你可以在这里找到你的答案:@RaymonJansen,我正在使用我的自定义控制台
类,因为在Xamarin.Forms
中,我不能使用.NET
的默认控制台
类。您好@Nkosi:感谢您的编辑。真不敢相信我错过了《等待》
-:)。但是试图理解在处理程序中使用局部变量是否会产生问题。没有问题。它只是帮助保持代码本身contained@Nkosi:哦,好的,谢谢。我在绞尽脑汁,想看看范围界定是否会成为一个问题。
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();