0
我正在使用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
}
}
我如何才能讓其他視圖模型的代碼重用,所以我不必一遍一遍實現同樣的事情?
我創建了一個實現INotifyDataErrorInfo類:
public class ViewModelValidation : INotifyDataErrorInfo
但是,當我想在我的視圖模型來使用它,它不工作:
public partial class AdministrationViewModel : ViewModelValidation
錯誤:
Partial declarations of 'SL.ViewModel.AdministrationViewModel' must not specify different base classes...
這是因爲在我的主視圖模型文件中我有一個MVVM-Light的基類:
public partial class AdministrationViewModel : ViewModelBase
解決此問題的任何幫助都是令人滿意的。