@@ -214,8 +214,21 @@ public async virtual Task<bool> TryLoadFromMemoryCacheAsync()
214
214
if ( result )
215
215
{
216
216
Logger . Debug ( string . Format ( "Image loaded from cache: {0}" , Key ) ) ;
217
- Parameters ? . OnSuccess ? . Invoke ( ImageInformation , LoadingResult . MemoryCache ) ;
218
- Parameters ? . OnFinish ? . Invoke ( this ) ;
217
+
218
+ if ( Configuration . ExecuteCallbacksOnUIThread && ( Parameters ? . OnSuccess != null || Parameters ? . OnFinish != null ) )
219
+ {
220
+ await MainThreadDispatcher . PostAsync ( ( ) =>
221
+ {
222
+ Parameters ? . OnSuccess ? . Invoke ( ImageInformation , LoadingResult . MemoryCache ) ;
223
+ Parameters ? . OnFinish ? . Invoke ( this ) ;
224
+ } ) ;
225
+ }
226
+ else
227
+ {
228
+ Parameters ? . OnSuccess ? . Invoke ( ImageInformation , LoadingResult . MemoryCache ) ;
229
+ Parameters ? . OnFinish ? . Invoke ( this ) ;
230
+ }
231
+
219
232
IsCompleted = true ;
220
233
}
221
234
else
@@ -249,7 +262,18 @@ await ShowPlaceholder(Parameters.LoadingPlaceholderPath, KeyForLoadingPlaceholde
249
262
else
250
263
{
251
264
Logger . Error ( string . Format ( "Image loading failed: {0}" , Key ) , ex ) ;
252
- Parameters ? . OnError ? . Invoke ( ex ) ;
265
+
266
+ if ( Configuration . ExecuteCallbacksOnUIThread && Parameters ? . OnError != null )
267
+ {
268
+ await MainThreadDispatcher . PostAsync ( ( ) =>
269
+ {
270
+ Parameters ? . OnError ? . Invoke ( ex ) ;
271
+ } ) ;
272
+ }
273
+ else
274
+ {
275
+ Parameters ? . OnError ? . Invoke ( ex ) ;
276
+ }
253
277
}
254
278
}
255
279
@@ -334,8 +358,19 @@ public async Task RunAsync()
334
358
335
359
if ( loadingResult == LoadingResult . Internet )
336
360
Logger ? . Debug ( string . Format ( "DownloadOnly success: {0}" , Key ) ) ;
337
-
338
- Parameters ? . OnSuccess ? . Invoke ( ImageInformation , loadingResult ) ;
361
+
362
+ if ( Configuration . ExecuteCallbacksOnUIThread && Parameters ? . OnSuccess != null )
363
+ {
364
+ await MainThreadDispatcher . PostAsync ( ( ) =>
365
+ {
366
+ Parameters ? . OnSuccess ? . Invoke ( ImageInformation , loadingResult ) ;
367
+ } ) ;
368
+ }
369
+ else
370
+ {
371
+ Parameters ? . OnSuccess ? . Invoke ( ImageInformation , loadingResult ) ;
372
+ }
373
+
339
374
return ;
340
375
}
341
376
@@ -362,7 +397,17 @@ public async Task RunAsync()
362
397
}
363
398
}
364
399
365
- Parameters ? . OnSuccess ? . Invoke ( ImageInformation , loadingResult ) ;
400
+ if ( Configuration . ExecuteCallbacksOnUIThread && Parameters ? . OnSuccess != null )
401
+ {
402
+ await MainThreadDispatcher . PostAsync ( ( ) =>
403
+ {
404
+ Parameters ? . OnSuccess ? . Invoke ( ImageInformation , loadingResult ) ;
405
+ } ) ;
406
+ }
407
+ else
408
+ {
409
+ Parameters ? . OnSuccess ? . Invoke ( ImageInformation , loadingResult ) ;
410
+ }
366
411
}
367
412
catch ( Exception ex )
368
413
{
@@ -381,7 +426,18 @@ public async Task RunAsync()
381
426
else
382
427
{
383
428
Logger . Error ( string . Format ( "Image loading failed: {0}" , Key ) , ex ) ;
384
- Parameters ? . OnError ? . Invoke ( ex ) ;
429
+
430
+ if ( Configuration . ExecuteCallbacksOnUIThread && Parameters ? . OnError != null )
431
+ {
432
+ await MainThreadDispatcher . PostAsync ( ( ) =>
433
+ {
434
+ Parameters ? . OnError ? . Invoke ( ex ) ;
435
+ } ) ;
436
+ }
437
+ else
438
+ {
439
+ Parameters ? . OnError ? . Invoke ( ex ) ;
440
+ }
385
441
386
442
try
387
443
{
@@ -405,7 +461,18 @@ await ShowPlaceholder(Parameters.ErrorPlaceholderPath, KeyForErrorPlaceholder,
405
461
{
406
462
using ( Parameters )
407
463
{
408
- Parameters ? . OnFinish ? . Invoke ( this ) ;
464
+ if ( Configuration . ExecuteCallbacksOnUIThread && Parameters ? . OnFinish != null )
465
+ {
466
+ await MainThreadDispatcher . PostAsync ( ( ) =>
467
+ {
468
+ Parameters ? . OnFinish ? . Invoke ( this ) ;
469
+ } ) ;
470
+ }
471
+ else
472
+ {
473
+ Parameters ? . OnFinish ? . Invoke ( this ) ;
474
+ }
475
+
409
476
ImageService . RemovePendingTask ( this ) ;
410
477
}
411
478
0 commit comments