c# - 仅接受用户输入中的某些字符
问题描述
我有这段代码,但应用程序进入中断模式并告诉我有一个未处理的异常:当用户输入除 A、B、C、DI 以外的任何内容时,值不能为空 已经解决了这个问题,但我的代码似乎不起作用.
private async void Confirm_Clicked(object sender, EventArgs e)
{
if (txtUserEntry.Text != null)
{
if (txtUserEntry.Text == "A")
{
await DisplayAlert("Wrong answer", "Sorry", "Next question");
await Navigation.PushModalAsync(new Quiz_Page2());
}
else if (txtUserEntry.Text == "B")
{
await DisplayAlert("Wrong answer", "Sorry", "Next question");
await Navigation.PushModalAsync(new Quiz_Page2());
}
else if (txtUserEntry.Text == "C")
{
await DisplayAlert("Wrong answer", "Sorry", "Next question");
await Navigation.PushModalAsync(new Quiz_Page2());
}
else if (txtUserEntry.Text == "D")
{
scoreSettings.GlobalScore++;
await DisplayAlert("Correct answer", "Well done", "Next question");
await Navigation.PushModalAsync(new Quiz_Page2());
}
else if (txtUserEntry.Text != "A" || txtUserEntry.Text != "B" || txtUserEntry.Text != "C" || txtUserEntry.Text != "D")
{
await DisplayAlert("Please type either A,B,C,D", "", "");
}
else await DisplayAlert("Please type either A,B,C,D", "", "");
}
}
}
解决方案
原因:
该方法DisplayAlert
有三个参数:
// title:
// The title of the alert dialog.
//
// message:
// The body text of the alert dialog.
//
// cancel:
// Text to be displayed on the 'Cancel' button.
它们是Nonnullable。因此,当您将其设置为 null 时,它会崩溃。
解决方案:
给出参数message
和cancel
值。如果你不想让它显示任何东西。你可以将它们设置为space
await DisplayAlert("Please type either A,B,C,D", " ", " ");
推荐阅读
- android - 模拟器:进程以退出代码 139 完成(被信号 11:sigsegv 中断)android studio
- javascript - 如何将 javascript 数组放入类对象的数组列表中?
- java - 如何获得内部视图项?
- .htaccess - .htaccess contineu 如果不满足重定向规则
- android - 使用 Update 或 execSQL 更新 SQLite 上的数据更好吗?
- sass - SASS:将伪类添加到祖父母&符号
- vba - 简单:循环下拉菜单,将值粘贴到新工作簿中
- c - 如何只杀死前台的子进程?
- c# - 子查询返回超过 1 个值,即使它们没有返回任何内容
- 2d - 在 Unreal-Engine 4 中,有没有办法在动画书完成动画后停止它?