-
Notifications
You must be signed in to change notification settings - Fork 372
Expand file tree
/
Copy pathEmbeddedResourceImageSource.cs
More file actions
56 lines (48 loc) · 1.9 KB
/
EmbeddedResourceImageSource.cs
File metadata and controls
56 lines (48 loc) · 1.9 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
using System;
using Xamarin.Forms;
using System.Reflection;
namespace FFImageLoading.Forms
{
/// <summary>
/// Embedded resource image source.
/// eg. resource://YourProject.Resource.Resource.png
/// eg. resource://YourProject.Resource.Resource.png?assembly=[FULL_ASSEMBLY_NAME]
/// </summary>
public class EmbeddedResourceImageSource : ImageSource
{
static string _cachedMainAssembly;
public EmbeddedResourceImageSource(Uri uri)
{
var text = uri.OriginalString;
if (string.IsNullOrWhiteSpace(uri.Query))
{
if (_cachedMainAssembly == null)
_cachedMainAssembly = Application.Current?.GetType()?.GetTypeAssemblyFullName();
Uri = new Uri(_cachedMainAssembly == null ? text : $"{text}?assembly={Uri.EscapeUriString(_cachedMainAssembly)}");
}
else if (!uri.Query.Contains("assembly=", StringComparison.OrdinalIgnoreCase))
{
var assemblyName = Application.Current?.GetType()?.GetTypeAssemblyFullName();
Uri = new Uri(assemblyName == null ? text : $"{text}?assembly={Uri.EscapeUriString(assemblyName)}");
}
else
{
Uri = uri;
}
}
public EmbeddedResourceImageSource(string resourceName, Assembly assembly)
{
Uri = new Uri($"resource://{resourceName}?assembly={Uri.EscapeUriString(assembly.FullName)}");
}
public static readonly BindableProperty UriProperty = BindableProperty.Create(nameof(Uri), typeof(Uri), typeof(EmbeddedResourceImageSource), default(Uri));
public Uri Uri
{
get => (Uri)GetValue(UriProperty);
set => SetValue(UriProperty, value);
}
public override string ToString()
{
return $"EmbeddedResource: {Uri}";
}
}
}