在我们的项目中,我们正在使用Devexpress sheduler conrol。我们的实体已正确映射,我们可以在视图中查看约会。一切正常,但现在我们需要在业务实体的setter中添加验证逻辑。现在,在运行时,如果违反业务规则,更改约会结束 - 应用程序崩溃,因为没有异常处理。在执行映射时,我无法找到捕获此错误的方法。
有人可以建议在从/到约会映射时如何/在哪里捕获此错误?
具有验证的财产代码:
public DateTime StartDateTime
{
get { return _startDateTime; }
set
{
if (_startDateTime != value)
{
OnSetStart(value);
_startDatetime = value;
NotifyPropertyChanged("StartDatetime");
}
}
}
public void OnSetSart(DateTime dateTime)
{
if(PisiblePeriodStart <= dateTime && dateTime <= PosiblePeriodEnd)
{
throw new Exception("Error")
}
}
它只是验证如何看起来的示例代码。可用期间是有效值的间隔。
答案 0 :(得分:0)
如果您不想复制验证逻辑,可以尝试这样的
bool IsStartValid;
public DateTime StartDate
{
get { return startDate; }
set
{
if (value > MaxValidDate)
{
IsStartValid = false;
throw new Exception("Error");//***
}
else
IsStartValid = true;
startDate=value;
}
}
您可以处理SchedulerControl的AppointmentDrop事件并检查IsStartValid属性
private void schedulerControl_AppointmentDrop(object sender, AppointmentDragEventArgs e)
{
YourClass temp = e.EditedAppointment.GetRow(schedulerStorage1) as YourClass;
if(temp.IsStartValid==false)
{
MessageBox.Show("Invalid Start");
e.Handled = true;
e.Allow = false;
}
// if (e.EditedAppointment.End < DateTime.Now)
// {
// MessageBox.Show("You can't move appointments to the past");
// e.Handled = true;
// e.Allow = false;
// }
}
AppointmentResizing事件相同
***这是例外吗?