首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >INotifyDataErrorInfo可重用方法

INotifyDataErrorInfo可重用方法
EN

Stack Overflow用户
提问于 2011-09-02 07:56:33
回答 1查看 1.3K关注 0票数 0

我使用的是MVVM,对于每个视图模型,我必须创建一个INotifyDataErrorInfo的实现,在某些情况下,它使用相同的方法来验证相同的属性类型。在本例中,我使用的是DateTime:

代码语言:javascript
复制
    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的类:

代码语言:javascript
复制
public class ViewModelValidation : INotifyDataErrorInfo

但是,当我想在我的视图模型中使用它时,它不起作用:

代码语言:javascript
复制
public partial class AdministrationViewModel : ViewModelValidation

错误:

代码语言:javascript
复制
Partial declarations of 'SL.ViewModel.AdministrationViewModel' must not specify different base classes...

这是因为在我的主视图模型文件中,我有一个来自MVVM的基类:

代码语言:javascript
复制
public partial class AdministrationViewModel : ViewModelBase

任何解决这个问题的帮助都是被认可的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-09-02 08:20:12

我自己想出来的。我从MVVM创建了一个基于ViewModelCommonViewModelBase类,并向它添加了INotifyDataErrorInfo接口:

代码语言:javascript
复制
public class ViewModelCommon : ViewModelBase, INotifyDataErrorInfo

然后,在我的视图模型代码中,而不是ViewModelBase,我只是使用了我的ViewModelCommon类:

代码语言:javascript
复制
public partial class AdministrationViewModel : ViewModelCommon

而且效果很好。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7280631

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档