-
Notifications
You must be signed in to change notification settings - Fork 372
Expand file tree
/
Copy pathFFImageLoadingImageViewHandler.cs
More file actions
69 lines (61 loc) · 2.43 KB
/
FFImageLoadingImageViewHandler.cs
File metadata and controls
69 lines (61 loc) · 2.43 KB
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
using System;
using System.Threading;
using System.Threading.Tasks;
using FFImageLoading.Work;
using Android.Widget;
using FFImageLoading.Forms.Handlers;
using Xamarin.Forms.Platform.Android;
//[assembly: Xamarin.Forms.ExportImageSourceHandler(typeof(Xamarin.Forms.FileImageSource), typeof(FFImageLoading.Forms.Platform.FFImageLoadingImageViewHandler))]
//[assembly: Xamarin.Forms.ExportImageSourceHandler(typeof(Xamarin.Forms.StreamImageSource), typeof(FFImageLoading.Forms.Platform.FFImageLoadingImageViewHandler))]
//[assembly: Xamarin.Forms.ExportImageSourceHandler(typeof(Xamarin.Forms.UriImageSource), typeof(FFImageLoading.Forms.Platform.FFImageLoadingImageViewHandler))]
//[assembly: Xamarin.Forms.ExportImageSourceHandler(typeof(FFImageLoading.Forms.EmbeddedResourceImageSource), typeof(FFImageLoading.Forms.Platform.FFImageLoadingImageViewHandler))]
//[assembly: Xamarin.Forms.ExportImageSourceHandler(typeof(FFImageLoading.Forms.DataUrlImageSource), typeof(FFImageLoading.Forms.Platform.FFImageLoadingImageViewHandler))]
namespace FFImageLoading.Forms.Platform
{
public class FFImageLoadingImageViewHandler : HandlerBase<ImageView>, IImageViewHandler
{
public Task LoadImageAsync(Xamarin.Forms.ImageSource imageSource, ImageView imageView, CancellationToken cancellationToken = default)
{
try
{
if (!IsValid(imageView))
return Task.CompletedTask;
var source = ImageSourceBinding.GetImageSourceBinding(imageSource, null);
if (source == null)
{
imageView.SetImageResource(Android.Resource.Color.Transparent);
return Task.CompletedTask;
}
return LoadImageAsync(source, imageSource, imageView, cancellationToken);
}
catch (Exception)
{
return Task.CompletedTask;
}
}
private static bool IsValid(ImageView imageView)
{
if (imageView == null || imageView.Handle == IntPtr.Zero)
return false;
#pragma warning disable CS0618 // Type or member is obsolete
var activity = imageView.Context as Android.App.Activity ?? (Android.App.Activity)Xamarin.Forms.Forms.Context;
#pragma warning restore CS0618 // Type or member is obsolete
if (activity != null)
{
if (activity.IsFinishing)
return false;
if (activity.IsDestroyed)
return false;
}
else
{
return false;
}
return true;
}
protected override IImageLoaderTask GetImageLoaderTask(TaskParameter parameters, ImageView imageView)
{
return parameters.Into(imageView) as IImageLoaderTask;
}
}
}