INotifyDataErrorInfo 可重用方法

发布于 12-02 17:59 字数 4377 浏览 2 评论 0原文

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

    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

感谢解决此问题的任何帮助。

I am using MVVM-Light and for every view model I have to create an implementation of the INotifyDataErrorInfo which in some cases uses same methods to validate same property types.
In this example I am using DateTime:

    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
    }
}

How could I make this code reusable in other view models, so I don't have to implement the same thing over and over again?

I created a class that implements INotifyDataErrorInfo :

public class ViewModelValidation : INotifyDataErrorInfo

But when I want to use it in my view model it doesn't work:

public partial class AdministrationViewModel : ViewModelValidation

Error:

Partial declarations of 'SL.ViewModel.AdministrationViewModel' must not specify different base classes...

this is because in my main view model file I have a base class from MVVM-Light:

public partial class AdministrationViewModel : ViewModelBase

Any help resolving this is appretiated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

骄兵必败2024-12-09 17:59:36

我自己想出来了。我从 MVVM-Light 创建了一个基于 ViewModelBaseViewModelCommon 类,并向其中添加了 INotifyDataErrorInfo 接口:

public class ViewModelCommon : ViewModelBase, INotifyDataErrorInfo

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

public partial class AdministrationViewModel : ViewModelCommon

它工作得很好。

I figured it out myself. I created a ViewModelCommon class based on ViewModelBase from MVVM-Light and added the INotifyDataErrorInfo interface to it:

public class ViewModelCommon : ViewModelBase, INotifyDataErrorInfo

Then in my view model code instead of ViewModelBase I just used my ViewModelCommon class:

public partial class AdministrationViewModel : ViewModelCommon

And it works just fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文