-
Notifications
You must be signed in to change notification settings - Fork 372
Expand file tree
/
Copy pathImageSourceConverter.cs
More file actions
55 lines (45 loc) · 1.72 KB
/
ImageSourceConverter.cs
File metadata and controls
55 lines (45 loc) · 1.72 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
using System;
using System.Globalization;
using Xamarin.Forms;
namespace FFImageLoading.Forms
{
public class ImageSourceConverter : TypeConverter, IValueConverter
{
public override bool CanConvertFrom(Type sourceType)
{
if (sourceType == null)
throw new ArgumentNullException(nameof(sourceType));
return sourceType == typeof(string);
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return ConvertFromInvariantString(value as string);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
public override object ConvertFromInvariantString(string value)
{
if (!(value is string text))
return null;
if (text.IsDataUrl())
{
return new DataUrlImageSource(text);
}
if (Uri.TryCreate(text, UriKind.Absolute, out var uri))
{
if (uri.Scheme.Equals("file", StringComparison.OrdinalIgnoreCase))
return ImageSource.FromFile(uri.LocalPath);
if (uri.Scheme.Equals("resource", StringComparison.OrdinalIgnoreCase))
return new EmbeddedResourceImageSource(uri);
return ImageSource.FromUri(uri);
}
if (!string.IsNullOrWhiteSpace(text))
{
return ImageSource.FromFile(text);
}
throw new InvalidOperationException(string.Format("Cannot convert \"{0}\" into {1}", value, typeof(ImageSource)));
}
}
}