3 using System.Reflection;
4 using System.Threading;
5 using System.Threading.Tasks;
7 namespace Tizen.NUI.Binding
9 [TypeConverter(typeof(ImageSourceConverter))]
10 internal abstract class ImageSource : Element
12 readonly object _synchandle = new object();
13 CancellationTokenSource _cancellationTokenSource;
15 TaskCompletionSource<bool> _completionSource;
17 readonly WeakEventManager _weakEventManager = new WeakEventManager();
19 protected ImageSource()
23 protected CancellationTokenSource CancellationTokenSource
25 get { return _cancellationTokenSource; }
28 if (_cancellationTokenSource == value)
30 if (_cancellationTokenSource != null)
31 _cancellationTokenSource.Cancel();
32 _cancellationTokenSource = value;
38 get { return _cancellationTokenSource != null; }
41 public virtual Task<bool> Cancel()
44 return Task.FromResult(false);
46 var tcs = new TaskCompletionSource<bool>();
47 TaskCompletionSource<bool> original = Interlocked.CompareExchange(ref _completionSource, tcs, null);
50 _cancellationTokenSource.Cancel();
58 public static ImageSource FromFile(string file)
60 return new FileImageSource { File = file };
63 public static ImageSource FromResource(string resource, Type resolvingType)
65 return FromResource(resource, resolvingType.GetTypeInfo().Assembly);
68 public static ImageSource FromResource(string resource, Assembly sourceAssembly = null)
71 sourceAssembly = sourceAssembly ?? Assembly.GetCallingAssembly();
73 if (sourceAssembly == null)
75 MethodInfo callingAssemblyMethod = typeof(Assembly).GetTypeInfo().GetDeclaredMethod("GetCallingAssembly");
76 if (callingAssemblyMethod != null)
78 sourceAssembly = (Assembly)callingAssemblyMethod.Invoke(null, new object[0]);
82 Internals.Log.Warning("Warning", "Can not find CallingAssembly, pass resolvingType to FromResource to ensure proper resolution");
87 return FromStream(() => sourceAssembly.GetManifestResourceStream(resource));
90 public static ImageSource FromStream(Func<Stream> stream)
92 // return new StreamImageSource { Stream = token => Task.Run(stream, token) };
96 public static ImageSource FromUri(Uri uri)
98 if (!uri.IsAbsoluteUri)
99 throw new ArgumentException("uri is relative");
100 // return new UriImageSource { Uri = uri };
104 public static implicit operator ImageSource(string source)
107 return Uri.TryCreate(source, UriKind.Absolute, out uri) && uri.Scheme != "file" ? FromUri(uri) : FromFile(source);
110 public static implicit operator ImageSource(Uri uri)
112 if (!uri.IsAbsoluteUri)
113 throw new ArgumentException("uri is relative");
117 protected void OnLoadingCompleted(bool cancelled)
119 if (!IsLoading || _completionSource == null)
122 TaskCompletionSource<bool> tcs = Interlocked.Exchange(ref _completionSource, null);
124 tcs.SetResult(cancelled);
128 CancellationTokenSource = null;
132 protected void OnLoadingStarted()
136 CancellationTokenSource = new CancellationTokenSource();
140 protected void OnSourceChanged()
142 _weakEventManager.HandleEvent(this, EventArgs.Empty, nameof(SourceChanged));
145 internal event EventHandler SourceChanged
147 add { _weakEventManager.AddEventHandler(nameof(SourceChanged), value); }
148 remove { _weakEventManager.RemoveEventHandler(nameof(SourceChanged), value); }