@@ -22,16 +22,42 @@ protected CommandBase(ILogger logger = null)
22
22
protected abstract void OnExecute ( object parameter ) ;
23
23
24
24
protected Func < object , bool > CanExecuteCondition { get ; private set ; }
25
+ protected Func < object , bool > OnExecuteCondition { get ; private set ; }
26
+ private bool RequireReEvaluationOnExecute => OnExecuteCondition != null ;
25
27
26
- protected void AddToCanExecuteEvaluation ( Func < object , bool > furtherCanExecuteEvaluation )
28
+ protected void AddToCanExecuteEvaluation ( Func < object , bool > furtherCanExecuteEvaluation , bool requireReevaluation = false )
27
29
{
28
30
if ( furtherCanExecuteEvaluation == null )
29
31
{
30
32
return ;
31
33
}
32
34
33
- var currentCanExecute = CanExecuteCondition ;
34
- CanExecuteCondition = ( parameter ) => currentCanExecute ( parameter ) && furtherCanExecuteEvaluation ( parameter ) ;
35
+ AddToCanExecuteEvaluation ( furtherCanExecuteEvaluation ) ;
36
+
37
+ if ( requireReevaluation )
38
+ {
39
+ AddToOnExecuteEvaluation ( furtherCanExecuteEvaluation ) ;
40
+ }
41
+ }
42
+
43
+ private void AddToCanExecuteEvaluation ( Func < object , bool > furtherCanExecuteEvaluation )
44
+ {
45
+ var currentCanExecuteCondition = CanExecuteCondition ;
46
+ CanExecuteCondition = ( parameter ) =>
47
+ currentCanExecuteCondition ( parameter ) && furtherCanExecuteEvaluation ( parameter ) ;
48
+ }
49
+
50
+ private void AddToOnExecuteEvaluation ( Func < object , bool > furtherCanExecuteEvaluation )
51
+ {
52
+ if ( OnExecuteCondition == null )
53
+ {
54
+ OnExecuteCondition = furtherCanExecuteEvaluation ;
55
+ }
56
+ else
57
+ {
58
+ var currentOnExecute = OnExecuteCondition ;
59
+ OnExecuteCondition = ( parameter ) => currentOnExecute ( parameter ) && furtherCanExecuteEvaluation ( parameter ) ;
60
+ }
35
61
}
36
62
37
63
public bool CanExecute ( object parameter )
@@ -53,15 +79,13 @@ public bool CanExecute(object parameter)
53
79
}
54
80
}
55
81
56
- protected virtual bool RequireReEvaluationOnExecute => false ;
57
-
58
82
public void Execute ( object parameter )
59
83
{
60
84
try
61
85
{
62
86
if ( RequireReEvaluationOnExecute )
63
87
{
64
- if ( ! CanExecute ( parameter ) )
88
+ if ( ! OnExecuteCondition ( parameter ) )
65
89
{
66
90
return ;
67
91
}
0 commit comments