检查条目是否为空

时间:2017-08-27 08:44:47

标签: c# xamarin xamarin.forms

我有一个如此定义的条目:

var nameEntry = new Entry(); nameEntry.SetBinding(Entry.TextProperty, "AccountName");

我想要检查一下,以便在此字段为空时不会保存。目前我的保存功能如下:

var saveButton = new Button { Text = "Save" };
saveButton.Clicked += async (sender, e) =>
{
     var application = (User)BindingContext;
     await App.Database.SaveApplicationAsync(application);
     await Navigation.PopAsync();                    
};

我试图通过这样做来验证:

var saveButton = new Button { Text = "Save" };
saveButton.Clicked += async (sender, e) =>
{
     if (nameEntry.Text.Length == 0)
     {
          await DisplayAlert("Oops", "Please enter an account name", "OK");
     }
     else
     {
          var application = (User)BindingContext;
          await App.Database.SaveApplicationAsync(application);
          await Navigation.PopAsync();
     }
};

然而,当我测试时,应用程序总是会中断。输出显示:

  

(16857):[ERROR] FATAL UNHANDLED EXCEPTION:System.NullReferenceException:对象引用未设置为对象的实例。

0 个答案:

没有答案