我有这段代码,但是应用程序进入中断模式,并告诉我有一个未处理的异常:每当用户输入除A,B,C,DI之外的其他任何值,该值都不能为null,但是我的代码没有似乎有效。
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", "", "");
}
}
}
答案 0 :(得分:0)
原因:
方法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.
它们是不可空的。因此,当您将其设置为null时,它将崩溃。
解决方案:
给参数message
和cancel
赋一个值。如果您不想显示任何内容,可以将其设置为space
await DisplayAlert("Please type either A,B,C,D", " ", " ");