-
Notifications
You must be signed in to change notification settings - Fork 0
/
ValidatableBindableBase.cs
113 lines (97 loc) · 4.41 KB
/
ValidatableBindableBase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Runtime.CompilerServices;
namespace Xamariners.Mvvm.Core
{
public abstract class ValidatableBindableBase :
BindableBase,
IValidatableBindableBase,
INotifyDataErrorInfo
{
protected readonly Class1.RecursiveBindableValidator _bindableValidator;
[JsonIgnore]
[NotMapped]
public bool IsValidationEnabled
{
get => this._bindableValidator.IsValidationEnabled;
set => this._bindableValidator.IsValidationEnabled = value;
}
[JsonIgnore]
[NotMapped]
public Class1.RecursiveBindableValidator Errors => this._bindableValidator;
[JsonIgnore]
[NotMapped]
public bool HasErrors => !this.ValidateProperties((object)null, (List<Tuple<string, bool, string>>)null);
public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged
{
add => this._bindableValidator.ErrorsChanged += value;
remove => this._bindableValidator.ErrorsChanged -= value;
}
public ValidatableBindableBase() => this._bindableValidator = new Class1.RecursiveBindableValidator((INotifyPropertyChanged)this);
public ReadOnlyDictionary<string, ReadOnlyCollection<string>> GetAllErrors() => this._bindableValidator.GetAllErrors();
public bool ValidateProperties(
object entityToValidate = null,
List<Tuple<string, bool, string>> validators = null)
{
return this._bindableValidator.ValidateProperties(entityToValidate, validators);
}
public bool ValidatePropertyList(Dictionary<string, object> properties) => this.ValidatePropertyList(properties, (Dictionary<string, string>)null);
public bool ValidatePropertyList(
Dictionary<string, object> properties,
Dictionary<string, string> propertiesDisplayName,
List<Tuple<string, bool, string>> validators = null)
{
return this._bindableValidator.ValidatePropertyList(properties, validators);
}
public bool ValidateProperty(string propertyName, object entityToValidate = null) => !this._bindableValidator.IsValidationEnabled || this._bindableValidator.ValidateProperty(propertyName, entityToValidate);
public void SetAllErrors(
IDictionary<string, ReadOnlyCollection<string>> entityErrors)
{
this._bindableValidator.SetAllErrors(entityErrors);
}
protected override bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
{
int num = base.SetProperty<T>(ref storage, value, propertyName) ? 1 : 0;
if (num == 0 || string.IsNullOrEmpty(propertyName) || !this._bindableValidator.IsValidationEnabled)
return num != 0;
this._bindableValidator.ValidateProperty(propertyName);
return num != 0;
}
protected bool SetPropertySafe<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
{
int num = this.SetPropertySafeInternal<T>(ref storage, value, propertyName) ? 1 : 0;
if (num == 0 || string.IsNullOrEmpty(propertyName) || !this._bindableValidator.IsValidationEnabled)
return num != 0;
this._bindableValidator.ValidateProperty(propertyName);
return num != 0;
}
private bool SetPropertySafeInternal<T>(ref T storage, T value, string propertyName)
{
if (EqualityComparer<T>.Default.Equals(storage, value))
return false;
storage = value;
this.RaisePropertyChangedSafeInternal(propertyName);
return true;
}
protected void RaisePropertyChangedSafeInternal(string propertyName)
{
try
{
this.RaisePropertyChanged(propertyName);
}
catch (Exception ex)
{
if (ex.Message.Contains("FFImageLoading"))
return;
throw;
}
}
public IEnumerable GetErrors(string propertyName) => !this.HasErrors ? (IEnumerable)Enumerable.Empty<string>() : (IEnumerable)this._bindableValidator[propertyName];
}
}