我尝试进行基本的表单验证以要求必须填写表单上的某些字段。我尝试对 ViewModel 进行验证检查,我有一个 ValidateForm 函数并将返回一个 ValidationMessage,但是,当 SaveCommand 调用它时不会再次回调当前视图,因此不会弹出错误消息警报视图。SaveCommand 完成后如何再次调用当前的视图模型?
private string _ValidationMessage;
public string ValidationMessage
{
get { return _ValidationMessage; }
set
{
_ValidationMessage = value;
RaisePropertyChanged(() => ValidationMessage);
}
}
private string ValideForm()
{
if (string.IsNullOrEmpty(FirstName) || string.IsNullOrEmpty(LastName) || string.IsNullOrEmpty(Email) || string.IsNullOrEmpty(ZipCode))
return "Fields with * are required.";
return null;
}
private MvxCommand _saveCommand;
public ICommand SaveCommand
{
get
{
_saveCommand = _saveCommand ?? new MvxCommand(SaveCommandHandler);
return _saveCommand;
}
}
private void SaveCommandHandler()
{
var validationMessage = ValideForm();
if (!string.IsNullOrEmpty(validationMessage))
{
ValidationMessage = validationMessage;
return;
}
ShowViewModel<NextScreenViewModel>();
}