MVVM-Light를 사용하고 모든 뷰 모델에 대해 INotifyDataErrorInfo 구현을 만들어야하는 경우가 있는데, 동일한 메서드를 사용하여 동일한 속성 유형의 유효성을 검사하는 경우가 있습니다. 이 예에서 나는 날짜 시간 사용하고 있습니다 :INotifyDataErrorInfo 재사용 가능한 메서드
using System;
using System.Collections.Generic;
using System.ComponentModel;
using SL.Resources;
namespace SL.ViewModel
{
public partial class AdministrationViewModel : INotifyDataErrorInfo
{
#region Validations
public void isDateToValid(DateTime? value, DateTime? dateFrom, string propertyName)
{
//check if null
if (value == null) AddError(propertyName, CommonErrors.DateNull_ERROR, false);
else RemoveError(propertyName, CommonErrors.DateNull_ERROR);
//check if min and max value
if (value < DateTime.MinValue || value > DateTime.MaxValue) AddError(propertyName, CommonErrors.DateNotValid_ERROR, false);
else RemoveError(propertyName, CommonErrors.DateNotValid_ERROR);
if (value < dateFrom) AddError(propertyName, CommonErrors.DateFromSmall_ERROR, false);
else RemoveError(propertyName, CommonErrors.DateFromSmall_ERROR);
}
public void IsDateValid(DateTime? value, string propertyName)
{
if (value == null) AddError(propertyName, CommonErrors.DateNull_ERROR, false);
else RemoveError(propertyName, CommonErrors.DateNull_ERROR);
if (value < DateTime.MinValue || value > DateTime.MaxValue) AddError(propertyName, CommonErrors.DateNotValid_ERROR, false);
else RemoveError(propertyName, CommonErrors.DateNotValid_ERROR);
}
#endregion
#region INotifyDataErrorInfo Members
public Dictionary<string, List<string>> errors = new Dictionary<string, List<string>>();
// Adds the specified error to the errors collection if it is not
// already present, inserting it in the first position if isWarning is
// false. Raises the ErrorsChanged event if the collection changes.
public void AddError(string propertyName, string error, bool isWarning)
{
if (!errors.ContainsKey(propertyName))
errors[propertyName] = new List<string>();
if (!errors[propertyName].Contains(error))
{
if (isWarning) errors[propertyName].Add(error);
else errors[propertyName].Insert(0, error);
RaiseErrorsChanged(propertyName);
}
}
// Removes the specified error from the errors collection if it is
// present. Raises the ErrorsChanged event if the collection changes.
public void RemoveError(string propertyName, string error)
{
if (errors.ContainsKey(propertyName) &&
errors[propertyName].Contains(error))
{
errors[propertyName].Remove(error);
if (errors[propertyName].Count == 0) errors.Remove(propertyName);
RaiseErrorsChanged(propertyName);
}
}
public void RaiseErrorsChanged(string propertyName)
{
if (ErrorsChanged != null)
ErrorsChanged(this, new DataErrorsChangedEventArgs(propertyName));
}
public event System.EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
public System.Collections.IEnumerable GetErrors(string propertyName)
{
if (string.IsNullOrEmpty(propertyName) ||
!errors.ContainsKey(propertyName)) return null;
return errors[propertyName];
}
public bool HasErrors
{
get { return errors.Count > 0; }
}
#endregion
}
}
이 어떻게 다른보기 모델이 코드 재사용을 만들 수를, 그래서 나는 또 다시 같은 일을 구현할 필요가 없습니다? 내가보기 모델을 사용할 때 작동하지 않습니다
public class ViewModelValidation : INotifyDataErrorInfo
:하지만
public partial class AdministrationViewModel : ViewModelValidation
오류 :
Partial declarations of 'SL.ViewModel.AdministrationViewModel' must not specify different base classes...
나는 INotifyDataErrorInfo를 구현하는 클래스를 생성
내 기본보기 모델 파일에 MVVM-Light의 기본 클래스가 있기 때문에 이것은 같습니다.
public partial class AdministrationViewModel : ViewModelBase
이 문제를 해결하려면 도움이 필요합니다.