diff options
Diffstat (limited to 'Xamarin.Forms.Platform.Tizen/Renderers/ImageRenderer.cs')
-rw-r--r-- | Xamarin.Forms.Platform.Tizen/Renderers/ImageRenderer.cs | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/Xamarin.Forms.Platform.Tizen/Renderers/ImageRenderer.cs b/Xamarin.Forms.Platform.Tizen/Renderers/ImageRenderer.cs new file mode 100644 index 00000000..ca06a669 --- /dev/null +++ b/Xamarin.Forms.Platform.Tizen/Renderers/ImageRenderer.cs @@ -0,0 +1,105 @@ +using System.Threading; +using System.Threading.Tasks; + +namespace Xamarin.Forms.Platform.Tizen +{ + public class ImageRenderer : ViewRenderer<Image, Native.Image> + { + public ImageRenderer() + { + RegisterPropertyHandler(Image.SourceProperty, UpdateSource); + RegisterPropertyHandler(Image.AspectProperty, UpdateAspect); + RegisterPropertyHandler(Image.IsOpaqueProperty, UpdateIsOpaque); + } + + protected override void OnElementChanged(ElementChangedEventArgs<Image> e) + { + if (Control == null) + { + var image = new Native.Image(Forms.Context.MainWindow); + SetNativeControl(image); + } + + base.OnElementChanged(e); + } + + async void UpdateSource() + { + ImageSource source = Element.Source; + + ((IImageController)Element).SetIsLoading(true); + + if (Control != null) + { + bool success = await Control.LoadFromImageSourceAsync(source); + if (!IsDisposed && success) + ((IVisualElementController)Element).NativeSizeChanged(); + } + + if (!IsDisposed) + ((IImageController)Element).SetIsLoading(false); + } + + void UpdateAspect() + { + Control.Aspect = Element.Aspect; + } + + void UpdateIsOpaque() + { + Control.IsOpaque = Element.IsOpaque; + } + } + + public interface IImageSourceHandler : IRegisterable + { + Task<bool> LoadImageAsync(Native.Image image, ImageSource imageSource, CancellationToken cancelationToken = default(CancellationToken)); + } + + public sealed class FileImageSourceHandler : IImageSourceHandler + { + public Task<bool> LoadImageAsync(Native.Image image, ImageSource imageSource, CancellationToken cancelationToken = default(CancellationToken)) + { + var filesource = imageSource as FileImageSource; + if (filesource != null) + { + string file = filesource.File; + if (!string.IsNullOrEmpty(file)) + return image.LoadAsync(ResourcePath.GetPath(file), cancelationToken); + } + return Task.FromResult<bool>(false); + } + } + + public sealed class StreamImageSourceHandler : IImageSourceHandler + { + public async Task<bool> LoadImageAsync(Native.Image image, ImageSource imageSource, CancellationToken cancelationToken = default(CancellationToken)) + { + var streamsource = imageSource as StreamImageSource; + if (streamsource != null && streamsource.Stream != null) + { + using (var streamImage = await ((IStreamImageSource)streamsource).GetStreamAsync(cancelationToken)) + { + if (streamImage != null) + return await image.LoadAsync(streamImage, cancelationToken); + } + } + return false; + } + } + + public sealed class UriImageSourceHandler : IImageSourceHandler + { + public Task<bool> LoadImageAsync(Native.Image image, ImageSource imageSource, CancellationToken cancelationToken = default(CancellationToken)) + { + var urisource = imageSource as UriImageSource; + if (urisource != null && urisource.Uri != null) + { + return image.LoadAsync(urisource.Uri, cancelationToken); + } + + return Task.FromResult<bool>(false); + } + } +} + |