3

我正在便携式库类中创建我的域对象。那些应该实施INotifyPropertChangedINotifyDataErrorInfo

所以,我的域类应该实现这个基类

public abstract class DomainObject : INotifyPropertyChanged, INotifyDataErrorInfo
{
    private ErrorsContainer<ValidationResult> errorsContainer;

    protected DomainObject() {}

    public event PropertyChangedEventHandler PropertyChanged;
    public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;

    public bool HasErrors
    {
        get { return this.ErrorsContainer.HasErrors; }
    }

    protected ErrorsContainer<ValidationResult> ErrorsContainer
    {
        get
        {
            if (this.errorsContainer == null)
            {
                this.errorsContainer =
                    new ErrorsContainer<ValidationResult>(pn => this.RaiseErrorsChanged(pn));
            }

            return this.errorsContainer;
        }
    }

    public IEnumerable GetErrors(string propertyName)
    {
        return this.errorsContainer.GetErrors(propertyName);
    }

    protected void RaisePropertyChanged(string propertyName)
    {
        this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
    }

    protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        var handler = this.PropertyChanged;
        if (handler != null)
        {
            handler(this, e);
        }
    }

    protected void ValidateProperty(string propertyName, object value)
    {
        if (string.IsNullOrEmpty(propertyName))
        {
            throw new ArgumentNullException("propertyName");
        }

        this.ValidateProperty(new ValidationContext(this, null, null) { MemberName = propertyName }, value);
    }

    protected virtual void ValidateProperty(ValidationContext validationContext, object value)
    {
        if (validationContext == null)
        {
            throw new ArgumentNullException("validationContext");
        }

        List<ValidationResult> validationResults = new List<ValidationResult>();
        Validator.TryValidateProperty(value, validationContext, validationResults);

        this.ErrorsContainer.SetErrors(validationContext.MemberName, validationResults);
    }

    protected void RaiseErrorsChanged(string propertyName)
    {
        this.OnErrorsChanged(new DataErrorsChangedEventArgs(propertyName));
    }

    protected virtual void OnErrorsChanged(DataErrorsChangedEventArgs e)
    {
        var handler = this.ErrorsChanged;
        if (handler != null)
        {
            handler(this, e);
        }
    }
}

但我意识到在这一行

        this.ValidateProperty(new ValidationContext(this, null, null)
           { MemberName = propertyName }, value);

我无法创建对象 ValidationContext 因为它没有任何构造函数。我该怎么做才能创建新的?

更新 根据我的智能感知,这包含。

#region Assembly System.ComponentModel.DataAnnotations.dll, v2.0.5.0
// C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETPortable\v4.0\Profile\Profile46\System.ComponentModel.DataAnnotations.dll
#endregion

using System;
using System.Collections.Generic;

namespace System.ComponentModel.DataAnnotations
{
    // Summary:
    //     Describes the context in which a validation check is performed.
    public sealed class ValidationContext
    {
        // Summary:
        //     Gets or sets the name of the member to validate.
        //
        // Returns:
        //     The name of the member to validate.
        public string DisplayName { get; set; }
        //
        // Summary:
        //     Gets the dictionary of key/value pairs that is associated with this context.
        //
        // Returns:
        //     The dictionary of the key/value pairs for this context.
        public IDictionary<object, object> Items { get; }
        //
        // Summary:
        //     Gets or sets the name of the member to validate.
        //
        // Returns:
        //     The name of the member to validate.
        public string MemberName { get; set; }
        //
        // Summary:
        //     Gets the object to validate.
        //
        // Returns:
        //     The object to validate.
        public object ObjectInstance { get; }
        //
        // Summary:
        //     Gets the type of the object to validate.
        //
        // Returns:
        //     The type of the object to validate.
        public Type ObjectType { get; }

        // Summary:
        //     Returns the service that provides custom validation.
        //
        // Parameters:
        //   serviceType:
        //     The type of the service to use for validation.
        //
        // Returns:
        //     An instance of the service, or null if the service is not available.
        public object GetService(Type serviceType);
    }
}
4

1 回答 1

4

这是我们错过的。在 Visual Studio 2012 的早期版本中,IServiceProvider 不可用,因为它已从 Windows 应用商店应用程序中删除。由于便携式在下面建模的方式,这意味着没有其他平台组合可以公开它。这导致任何对它有依赖关系的东西都被删除,因此是 ValidationContext 构造函数。后来我们又把 IServiceProvider 加回来了,错过了这个构造函数。我已经在内部提交了一个错误,我们将看看是否可以在未来的版本中重新添加它。

要解决此问题,您有几个选择:

1) 面向 .NET Framework 4.5 和 Silverlight 5。这些版本添加了不依赖于 IServiceProvider 的新构造函数。

2)使用反射调用构造函数。请注意,这仅适用于 .NET Framework 和 Silverlight。它在 Windows 应用商店应用程序中不起作用,因为它不公开此构造函数(它将抛出 InvalidOperationException)。

3) 让平台特定项目(即 .NET Framework 4.0 或 Silverlight 4)项目自己创建 ValidationContext,并将其注入可移植库中。您可以通过某种依赖注入来完成此操作,也可以通过我在将现有库转换为 PCL部分下的使用可移植类库创建连续客户端中调用的平台适配器模式来做到这一点。

于 2012-10-08T21:40:09.413 回答