summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.WP8/ImageRenderer.cs
blob: ebc50adca008b2963a0e73596b5e838feeaad3cb (plain)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
´╗┐using System;
using System.ComponentModel;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace Xamarin.Forms.Platform.WinPhone
{
	internal static class ImageExtensions
	{
		public static Stretch ToStretch(this Aspect aspect)
		{
			switch (aspect)
			{
				case Aspect.Fill:
					return Stretch.Fill;
				case Aspect.AspectFill:
					return Stretch.UniformToFill;
				default:
				case Aspect.AspectFit:
					return Stretch.Uniform;
			}
		}
	}

	public class ImageRenderer : ViewRenderer<Image, System.Windows.Controls.Image>
	{
		IElementController ElementController => Element as IElementController;

		public override SizeRequest GetDesiredSize(double widthConstraint, double heightConstraint)
		{
			// Someone reported a NRE happening in this method which can only be explained by Control being null
			// which only happens at the very beginning of the view lifecycle. Honest I have no idea how this might 
			// happen because it really shouldn't measure at that point. Add check anyway and live in fear...
			if (Control?.Source == null)
				return new SizeRequest();

			var result = new Size { Width = ((BitmapImage)Control.Source).PixelWidth, Height = ((BitmapImage)Control.Source).PixelHeight };

			return new SizeRequest(result);
		}

		protected override void OnElementChanged(ElementChangedEventArgs<Image> e)
		{
			base.OnElementChanged(e);

			if (e.NewElement != null)
			{
				if (Control == null)
				{
					var image = new System.Windows.Controls.Image();
					SetNativeControl(image);
				}

				SetSource(Control);
				SetAspect(Control);
			}
		}

		protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			base.OnElementPropertyChanged(sender, e);

			if (e.PropertyName == Image.SourceProperty.PropertyName)
				SetSource(Control);
			else if (e.PropertyName == Image.AspectProperty.PropertyName)
				SetAspect(Control);
		}

		void SetAspect(System.Windows.Controls.Image image)
		{
			Aspect aspect = Element.Aspect;

			image.Stretch = aspect.ToStretch();
		}

		async void SetSource(System.Windows.Controls.Image image)
		{
			((IImageController)Element).SetIsLoading(true);

			ImageSource source = Element.Source;
			IImageSourceHandler handler;
			if (source != null && (handler = Registrar.Registered.GetHandler<IImageSourceHandler>(source.GetType())) != null)
			{
				System.Windows.Media.ImageSource imagesource;
				try
				{
					imagesource = await handler.LoadImageAsync(source);
				}
				catch (TaskCanceledException)
				{
					imagesource = null;
				}
				image.Source = imagesource;
				// if you dont at least measure the thing once it wont load the image
				// then the whole thing falls over.
				image.Measure(new System.Windows.Size(100, 100));
				((IVisualElementController)Element).NativeSizeChanged();
			}
			else
				image.Source = null;

			((IImageController)Element).SetIsLoading(false);
		}
	}

	public interface IImageSourceHandler : IRegisterable
	{
		Task<System.Windows.Media.ImageSource> LoadImageAsync(ImageSource imagesoure, CancellationToken cancelationToken = default(CancellationToken));
	}

	public sealed class FileImageSourceHandler : IImageSourceHandler
	{
		public Task<System.Windows.Media.ImageSource> LoadImageAsync(ImageSource imagesoure, CancellationToken cancelationToken = new CancellationToken())
		{
			System.Windows.Media.ImageSource image = null;
			var filesource = imagesoure as FileImageSource;
			if (filesource != null)
			{
				string file = filesource.File;
				image = new BitmapImage(new Uri("/" + file, UriKind.Relative));
			}
			return Task.FromResult(image);
		}
	} 

	public sealed class StreamImagesourceHandler : IImageSourceHandler
	{
		public async Task<System.Windows.Media.ImageSource> LoadImageAsync(ImageSource imagesource, CancellationToken cancelationToken = new CancellationToken())
		{
			BitmapImage bitmapimage = null;

			var streamsource = imagesource as StreamImageSource;
			if (streamsource != null && streamsource.Stream != null)
			{
				using (Stream stream = await ((IStreamImageSource)streamsource).GetStreamAsync(cancelationToken))
				{
					bitmapimage = new BitmapImage();
					bitmapimage.SetSource(stream);
				}
			}
			return (System.Windows.Media.ImageSource)bitmapimage;
		}
	}

	public sealed class ImageLoaderSourceHandler : IImageSourceHandler
	{
		public async Task<System.Windows.Media.ImageSource> LoadImageAsync(ImageSource imagesoure, CancellationToken cancelationToken = new CancellationToken())
		{
			BitmapImage bitmapimage = null;
			var imageLoader = imagesoure as UriImageSource;
			if (imageLoader != null && imageLoader.Uri != null)
			{
				using (Stream streamimage = await imageLoader.GetStreamAsync(cancelationToken))
				{
					if (streamimage != null && streamimage.CanRead)
					{
						bitmapimage = new BitmapImage();
						bitmapimage.SetSource(streamimage);
					}
				}
			}
			return bitmapimage;
		}
	}
}