diff options
author | Jason Smith <jason.smith@xamarin.com> | 2016-03-22 13:02:25 -0700 |
---|---|---|
committer | Jason Smith <jason.smith@xamarin.com> | 2016-03-22 16:13:41 -0700 |
commit | 17fdde66d94155fc62a034fa6658995bef6fd6e5 (patch) | |
tree | b5e5073a2a7b15cdbe826faa5c763e270a505729 /Xamarin.Forms.Core/StreamImageSource.cs | |
download | xamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.tar.gz xamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.tar.bz2 xamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.zip |
Initial import
Diffstat (limited to 'Xamarin.Forms.Core/StreamImageSource.cs')
-rw-r--r-- | Xamarin.Forms.Core/StreamImageSource.cs | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/Xamarin.Forms.Core/StreamImageSource.cs b/Xamarin.Forms.Core/StreamImageSource.cs new file mode 100644 index 00000000..ae59ea3c --- /dev/null +++ b/Xamarin.Forms.Core/StreamImageSource.cs @@ -0,0 +1,47 @@ +using System; +using System.IO; +using System.Threading; +using System.Threading.Tasks; + +namespace Xamarin.Forms +{ + public class StreamImageSource : ImageSource + { + public static readonly BindableProperty StreamProperty = BindableProperty.Create("Stream", typeof(Func<CancellationToken, Task<Stream>>), typeof(StreamImageSource), + default(Func<CancellationToken, Task<Stream>>)); + + public virtual Func<CancellationToken, Task<Stream>> Stream + { + get { return (Func<CancellationToken, Task<Stream>>)GetValue(StreamProperty); } + set { SetValue(StreamProperty, value); } + } + + protected override void OnPropertyChanged(string propertyName) + { + if (propertyName == StreamProperty.PropertyName) + OnSourceChanged(); + base.OnPropertyChanged(propertyName); + } + + internal async Task<Stream> GetStreamAsync(CancellationToken userToken = default(CancellationToken)) + { + if (Stream == null) + return null; + + OnLoadingStarted(); + userToken.Register(CancellationTokenSource.Cancel); + Stream stream = null; + try + { + stream = await Stream(CancellationTokenSource.Token); + OnLoadingCompleted(false); + } + catch (OperationCanceledException) + { + OnLoadingCompleted(true); + throw; + } + return stream; + } + } +}
\ No newline at end of file |