回答編集履歴
1
見直しキャンペーン中
test
CHANGED
@@ -1,111 +1,56 @@
|
|
1
1
|
検索すると`ReactiveCommand`にする例が多いですが、`ToReactiveCommand`するか`ToReactiveProperty`するか程度の違いです。
|
2
|
-
|
3
2
|
コマンドの場合はエラーがないときに実行したいですから、判定は逆になることが多いですね。
|
4
3
|
|
5
4
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
```
|
5
|
+
```cs
|
10
|
-
|
11
6
|
using Reactive.Bindings;
|
12
|
-
|
13
7
|
using System;
|
14
|
-
|
15
8
|
using System.ComponentModel.DataAnnotations;
|
16
|
-
|
17
9
|
using System.Linq;
|
18
|
-
|
19
10
|
using System.Reactive.Linq;
|
20
11
|
|
21
|
-
|
22
|
-
|
23
12
|
namespace Questions357340
|
24
|
-
|
25
13
|
{
|
26
|
-
|
27
14
|
class ViewModel
|
28
|
-
|
29
15
|
{
|
30
|
-
|
31
16
|
[Required] public ReactiveProperty<string> A { get; }
|
32
|
-
|
33
17
|
[Range(1, 10)] public ReactiveProperty<int> B { get; }
|
34
|
-
|
35
18
|
public ReadOnlyReactiveProperty<bool> AnyErrors { get; }
|
36
|
-
|
37
19
|
public bool AnyErrorsField;
|
38
20
|
|
39
|
-
|
40
|
-
|
41
21
|
public ViewModel()
|
42
|
-
|
43
22
|
{
|
44
|
-
|
45
23
|
A = new ReactiveProperty<string>().SetValidateAttribute(() => A);
|
46
|
-
|
47
24
|
B = new ReactiveProperty<int>().SetValidateAttribute(() => B);
|
48
|
-
|
49
25
|
IObservable<bool>[] errors = { A.ObserveHasErrors, B.ObserveHasErrors, };
|
50
26
|
|
51
|
-
|
52
|
-
|
53
27
|
AnyErrors = errors.CombineLatest(x => x.Any(y => y)).ToReadOnlyReactiveProperty();
|
54
|
-
|
55
28
|
//AnyErrors = errors.CombineLatestValuesAreAllFalse().Inverse().ToReadOnlyReactiveProperty();
|
56
29
|
|
57
|
-
|
58
|
-
|
59
30
|
errors.CombineLatest(x => x.Any(y => y)).Subscribe(x => AnyErrorsField = x); // 後始末省略
|
60
|
-
|
61
31
|
AnyErrorsField = AnyErrors.Value; // 初期値は知りようがないのでセット
|
62
|
-
|
63
32
|
}
|
64
|
-
|
65
33
|
}
|
66
34
|
|
67
|
-
|
68
|
-
|
69
35
|
class Program
|
70
|
-
|
71
36
|
{
|
72
|
-
|
73
37
|
static void Main()
|
74
|
-
|
75
38
|
{
|
76
|
-
|
77
39
|
var vm = new ViewModel();
|
78
|
-
|
79
40
|
Console.WriteLine($"A.HasErrors:{vm.A.HasErrors}, B.HasErrors:{vm.B.HasErrors}, AnyErrors:{vm.AnyErrors.Value}, AnyErrorsField:{vm.AnyErrorsField}");
|
80
41
|
|
81
|
-
|
82
|
-
|
83
42
|
vm.A.Value = "hoge";
|
84
|
-
|
85
43
|
Console.WriteLine($"A.HasErrors:{vm.A.HasErrors}, B.HasErrors:{vm.B.HasErrors}, AnyErrors:{vm.AnyErrors.Value}, AnyErrorsField:{vm.AnyErrorsField}");
|
86
44
|
|
87
|
-
|
88
|
-
|
89
45
|
vm.B.Value = 1;
|
90
|
-
|
91
46
|
Console.WriteLine($"A.HasErrors:{vm.A.HasErrors}, B.HasErrors:{vm.B.HasErrors}, AnyErrors:{vm.AnyErrors.Value}, AnyErrorsField:{vm.AnyErrorsField}");
|
92
|
-
|
93
47
|
}
|
94
|
-
|
95
48
|
}
|
96
|
-
|
97
49
|
}
|
50
|
+
```
|
98
51
|
|
99
52
|
```
|
100
|
-
|
53
|
+
A.HasErrors:True, B.HasErrors:True, AnyErrors:True, AnyErrorsField:True
|
101
|
-
|
54
|
+
A.HasErrors:False, B.HasErrors:True, AnyErrors:True, AnyErrorsField:True
|
102
|
-
|
55
|
+
A.HasErrors:False, B.HasErrors:False, AnyErrors:False, AnyErrorsField:False
|
103
56
|
```
|
104
|
-
|
105
|
-
A.HasErrors:True, B.HasErrors:True, AnyErrors:True, AnyErrorsField:True
|
106
|
-
|
107
|
-
A.HasErrors:False, B.HasErrors:True, AnyErrors:True, AnyErrorsField:True
|
108
|
-
|
109
|
-
A.HasErrors:False, B.HasErrors:False, AnyErrors:False, AnyErrorsField:False
|
110
|
-
|
111
|
-
```
|