33using System . ComponentModel ;
44using System . Linq ;
55using System . Threading . Tasks ;
6+ using Prism . Common ;
67
78namespace Prism . Dialogs ;
89
@@ -13,8 +14,8 @@ namespace Prism.Dialogs;
1314public readonly struct DialogCallback
1415{
1516private readonly bool _empty = false ;
16- private readonly List < MulticastDelegate > _callbacks = new List < MulticastDelegate > ( ) ;
17- private readonly List < MulticastDelegate > _errorCallbacks = new List < MulticastDelegate > ( ) ;
17+ private readonly List < MulticastDelegate > _callbacks = new ( ) ;
18+ private readonly MulticastExceptionHandler _errorCallbacks = new ( ) ;
1819
1920/// <summary>
2021/// Creates a new instance of a DialogCallback
@@ -24,7 +25,10 @@ public DialogCallback()
2425{
2526}
2627
27- private DialogCallback ( bool empty ) => _empty = empty ;
28+ private DialogCallback ( bool empty )
29+ {
30+ _empty = empty ;
31+ }
2832
2933/// <summary>
3034/// Invokes the Delegates based on a specific Exception that was encountered.
@@ -43,26 +47,21 @@ public Task Invoke(Exception ex) =>
4347[ EditorBrowsable ( EditorBrowsableState . Never ) ]
4448public async Task Invoke ( IDialogResult result )
4549{
46- if ( _empty || ( ! _callbacks . Any ( ) && ! _errorCallbacks . Any ( ) ) )
50+ if ( _empty )
51+ {
4752return ;
48-
49- if ( result . Exception is notnull )
53+ }
54+ else if ( result . Exception is notnull && _errorCallbacks . CanHandle ( result . Exception ) )
5055{
51- // process error callbacks
52- if ( _errorCallbacks . Any ( ) )
53- {
54- foreach ( MulticastDelegate errorCallback in _errorCallbacks )
55- {
56- await Process ( errorCallback , result ) ;
57- }
58- return ;
59- }
56+ await _errorCallbacks . HandleAsync ( result . Exception , result ) ;
57+ return ;
6058}
61-
62-
63- foreach ( var callback in _callbacks )
59+ else if ( _callbacks . Any ( ) )
6460{
65- await Process ( callback , result ) ;
61+ foreach ( var callback in _callbacks )
62+ {
63+ await Process ( callback , result ) ;
64+ }
6665}
6766}
6867
@@ -72,14 +71,10 @@ private static async Task Process(MulticastDelegate @delegate, IDialogResult res
7271action ( ) ;
7372else if ( @delegate is Action < IDialogResult > actionResult )
7473actionResult ( result ) ;
75- else if ( @delegate is Action < Exception > actionException && result . Exception is notnull )
76- actionException ( result . Exception ) ;
7774else if ( @delegate is Func < Task > func )
7875await func ( ) ;
7976else if ( @delegate is Func < IDialogResult , Task > funcResult )
8077await funcResult ( result ) ;
81- else if ( @delegate is Func < Exception , Task > taskException && result . Exception is notnull )
82- await taskException ( result . Exception ) ;
8378}
8479
8580/// <summary>
@@ -117,7 +112,19 @@ public DialogCallback OnClose(Action<IDialogResult> action)
117112/// <returns></returns>
118113public DialogCallback OnError ( Action action )
119114{
120- _errorCallbacks . Add ( action ) ;
115+ _errorCallbacks . Register < Exception > ( action ) ;
116+ return this ;
117+ }
118+
119+ /// <summary>
120+ /// Provides a delegate callback method when an Exception is encountered
121+ /// </summary>
122+ /// <param name="action">The callback</param>
123+ /// <returns></returns>
124+ public DialogCallback OnError < TException > ( Action action )
125+ where TException : Exception
126+ {
127+ _errorCallbacks . Register < TException > ( action ) ;
121128return this ;
122129}
123130
@@ -128,7 +135,19 @@ public DialogCallback OnError(Action action)
128135/// <returns></returns>
129136public DialogCallback OnError ( Action < Exception > action )
130137{
131- _errorCallbacks . Add ( action ) ;
138+ _errorCallbacks . Register < Exception > ( action ) ;
139+ return this ;
140+ }
141+
142+ /// <summary>
143+ /// Provides a delegate callback method when an Exception is encountered
144+ /// </summary>
145+ /// <param name="action">The callback</param>
146+ /// <returns></returns>
147+ public DialogCallback OnError < TException > ( Action < TException > action )
148+ where TException : Exception
149+ {
150+ _errorCallbacks . Register < TException > ( action ) ;
132151return this ;
133152}
134153
@@ -137,9 +156,10 @@ public DialogCallback OnError(Action<Exception> action)
137156/// </summary>
138157/// <param name="action">The callback</param>
139158/// <returns></returns>
140- public DialogCallback OnError ( Action < IDialogResult > action )
159+ public DialogCallback OnError < TException > ( Action < TException , IDialogResult > action )
160+ where TException : Exception
141161{
142- _errorCallbacks . Add ( action ) ;
162+ _errorCallbacks . Register < TException > ( action ) ;
143163return this ;
144164}
145165
@@ -172,7 +192,19 @@ public DialogCallback OnCloseAsync(Func<IDialogResult, Task> func)
172192/// <returns></returns>
173193public DialogCallback OnErrorAsync ( Func < Task > func )
174194{
175- _errorCallbacks . Add ( func ) ;
195+ _errorCallbacks . Register < Exception > ( func ) ;
196+ return this ;
197+ }
198+
199+ /// <summary>
200+ /// Provides an asynchronous delegate callback method when an Exception is encountered
201+ /// </summary>
202+ /// <param name="func">The callback</param>
203+ /// <returns></returns>
204+ public DialogCallback OnErrorAsync < TException > ( Func < Task > func )
205+ where TException : Exception
206+ {
207+ _errorCallbacks . Register < TException > ( func ) ;
176208return this ;
177209}
178210
@@ -183,7 +215,19 @@ public DialogCallback OnErrorAsync(Func<Task> func)
183215/// <returns></returns>
184216public DialogCallback OnErrorAsync ( Func < Exception , Task > func )
185217{
186- _errorCallbacks . Add ( func ) ;
218+ _errorCallbacks . Register < Exception > ( func ) ;
219+ return this ;
220+ }
221+
222+ /// <summary>
223+ /// Provides an asynchronous delegate callback method when an Exception is encountered
224+ /// </summary>
225+ /// <param name="func">The callback</param>
226+ /// <returns></returns>
227+ public DialogCallback OnErrorAsync < TException > ( Func < TException , Task > func )
228+ where TException : Exception
229+ {
230+ _errorCallbacks . Register < TException > ( func ) ;
187231return this ;
188232}
189233
@@ -192,9 +236,10 @@ public DialogCallback OnErrorAsync(Func<Exception, Task> func)
192236/// </summary>
193237/// <param name="func">The callback</param>
194238/// <returns></returns>
195- public DialogCallback OnErrorAsync ( Func < IDialogResult , Task > func )
239+ public DialogCallback OnErrorAsync < TException > ( Func < TException , IDialogResult , Task > func )
240+ where TException : Exception
196241{
197- _errorCallbacks . Add ( func ) ;
242+ _errorCallbacks . Register < TException > ( func ) ;
198243return this ;
199244}
200245}