summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.WinRT
diff options
context:
space:
mode:
authorE.Z. Hart <hartez@users.noreply.github.com>2016-07-18 17:16:47 -0600
committerJason Smith <jason.smith@xamarin.com>2016-07-18 16:16:47 -0700
commit272033723ea275ceb8a288fa605eafd035c79f2d (patch)
treeabc639d0f5627f3877f9e237e473bda168e9e030 /Xamarin.Forms.Platform.WinRT
parentc9da550ce4987e01c1bbce9d0a17b860e1d300b3 (diff)
downloadxamarin-forms-272033723ea275ceb8a288fa605eafd035c79f2d.tar.gz
xamarin-forms-272033723ea275ceb8a288fa605eafd035c79f2d.tar.bz2
xamarin-forms-272033723ea275ceb8a288fa605eafd035c79f2d.zip
Windows image loader error handling (#260)
* Repros for various image issues * Log image loading errors * Better repro instructions and user interface * Image loading tests now running on WinRT/UWP phone/tablet/desktop * Move FailImageSource into shared project * Move FailImageSource into shared project * Update docs
Diffstat (limited to 'Xamarin.Forms.Platform.WinRT')
-rw-r--r--Xamarin.Forms.Platform.WinRT/FileImageSourceHandler.cs4
-rw-r--r--Xamarin.Forms.Platform.WinRT/IImageSourceHandler.cs2
-rw-r--r--Xamarin.Forms.Platform.WinRT/ImageRenderer.cs35
-rw-r--r--Xamarin.Forms.Platform.WinRT/Properties/AssemblyInfo.cs4
-rw-r--r--Xamarin.Forms.Platform.WinRT/StreamImagesourceHandler.cs2
-rw-r--r--Xamarin.Forms.Platform.WinRT/UriImageSourceHandler.cs (renamed from Xamarin.Forms.Platform.WinRT/ImageLoaderSourceHandler.cs)11
-rw-r--r--Xamarin.Forms.Platform.WinRT/WindowsBasePlatformServices.cs9
-rw-r--r--Xamarin.Forms.Platform.WinRT/Xamarin.Forms.Platform.WinRT.csproj2
8 files changed, 47 insertions, 22 deletions
diff --git a/Xamarin.Forms.Platform.WinRT/FileImageSourceHandler.cs b/Xamarin.Forms.Platform.WinRT/FileImageSourceHandler.cs
index c9c6f16b..2e1a907a 100644
--- a/Xamarin.Forms.Platform.WinRT/FileImageSourceHandler.cs
+++ b/Xamarin.Forms.Platform.WinRT/FileImageSourceHandler.cs
@@ -13,10 +13,10 @@ namespace Xamarin.Forms.Platform.WinRT
{
public sealed class FileImageSourceHandler : IImageSourceHandler
{
- public Task<Windows.UI.Xaml.Media.ImageSource> LoadImageAsync(ImageSource imagesoure, CancellationToken cancellationToken = new CancellationToken())
+ public Task<Windows.UI.Xaml.Media.ImageSource> LoadImageAsync(ImageSource imagesource, CancellationToken cancellationToken = new CancellationToken())
{
Windows.UI.Xaml.Media.ImageSource image = null;
- var filesource = imagesoure as FileImageSource;
+ var filesource = imagesource as FileImageSource;
if (filesource != null)
{
string file = filesource.File;
diff --git a/Xamarin.Forms.Platform.WinRT/IImageSourceHandler.cs b/Xamarin.Forms.Platform.WinRT/IImageSourceHandler.cs
index 91eff534..457d48e4 100644
--- a/Xamarin.Forms.Platform.WinRT/IImageSourceHandler.cs
+++ b/Xamarin.Forms.Platform.WinRT/IImageSourceHandler.cs
@@ -11,6 +11,6 @@ namespace Xamarin.Forms.Platform.WinRT
{
public interface IImageSourceHandler : IRegisterable
{
- Task<Windows.UI.Xaml.Media.ImageSource> LoadImageAsync(ImageSource imagesoure, CancellationToken cancellationToken = default(CancellationToken));
+ Task<Windows.UI.Xaml.Media.ImageSource> LoadImageAsync(ImageSource imagesource, CancellationToken cancellationToken = default(CancellationToken));
}
} \ No newline at end of file
diff --git a/Xamarin.Forms.Platform.WinRT/ImageRenderer.cs b/Xamarin.Forms.Platform.WinRT/ImageRenderer.cs
index 370da329..fa676c18 100644
--- a/Xamarin.Forms.Platform.WinRT/ImageRenderer.cs
+++ b/Xamarin.Forms.Platform.WinRT/ImageRenderer.cs
@@ -1,5 +1,6 @@
using System;
using System.ComponentModel;
+using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
@@ -34,12 +35,13 @@ namespace Xamarin.Forms.Platform.WinRT
if (Control != null)
{
Control.ImageOpened -= OnImageOpened;
+ Control.ImageFailed -= OnImageFailed;
}
base.Dispose(disposing);
}
- protected override void OnElementChanged(ElementChangedEventArgs<Image> e)
+ protected override async void OnElementChanged(ElementChangedEventArgs<Image> e)
{
base.OnElementChanged(e);
@@ -49,20 +51,21 @@ namespace Xamarin.Forms.Platform.WinRT
{
var image = new Windows.UI.Xaml.Controls.Image();
image.ImageOpened += OnImageOpened;
+ image.ImageFailed += OnImageFailed;
SetNativeControl(image);
}
- UpdateSource();
+ await UpdateSource();
UpdateAspect();
}
}
- protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
+ protected override async void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == Image.SourceProperty.PropertyName)
- UpdateSource();
+ await UpdateSource();
else if (e.PropertyName == Image.AspectProperty.PropertyName)
UpdateAspect();
}
@@ -87,6 +90,14 @@ namespace Xamarin.Forms.Platform.WinRT
{
RefreshImage();
}
+
+ ((IImageController)Element)?.SetIsLoading(false);
+ }
+
+ void OnImageFailed(object sender, ExceptionRoutedEventArgs exceptionRoutedEventArgs)
+ {
+ Log.Warning("Image Loading", $"Image failed to load: {exceptionRoutedEventArgs.ErrorMessage}" );
+ ((IImageController)Element)?.SetIsLoading(false);
}
void RefreshImage()
@@ -109,7 +120,7 @@ namespace Xamarin.Forms.Platform.WinRT
}
}
- async void UpdateSource()
+ async Task UpdateSource()
{
((IImageController)Element).SetIsLoading(true);
@@ -117,7 +128,8 @@ namespace Xamarin.Forms.Platform.WinRT
IImageSourceHandler handler;
if (source != null && (handler = Registrar.Registered.GetHandler<IImageSourceHandler>(source.GetType())) != null)
{
- Windows.UI.Xaml.Media.ImageSource imagesource;
+ Windows.UI.Xaml.Media.ImageSource imagesource = null;
+
try
{
imagesource = await handler.LoadImageAsync(source);
@@ -126,18 +138,25 @@ namespace Xamarin.Forms.Platform.WinRT
{
imagesource = null;
}
+ catch (Exception ex)
+ {
+ Log.Warning("Image Loading", $"Error updating image source: {ex}");
+ }
// In the time it takes to await the imagesource, some zippy little app
// might have disposed of this Image already.
if (Control != null)
+ {
Control.Source = imagesource;
+ }
RefreshImage();
}
else
+ {
Control.Source = null;
-
- ((IImageController)Element)?.SetIsLoading(false);
+ ((IImageController)Element)?.SetIsLoading(false);
+ }
}
}
} \ No newline at end of file
diff --git a/Xamarin.Forms.Platform.WinRT/Properties/AssemblyInfo.cs b/Xamarin.Forms.Platform.WinRT/Properties/AssemblyInfo.cs
index d1975076..83023289 100644
--- a/Xamarin.Forms.Platform.WinRT/Properties/AssemblyInfo.cs
+++ b/Xamarin.Forms.Platform.WinRT/Properties/AssemblyInfo.cs
@@ -37,8 +37,8 @@ using Xamarin.Forms.Platform.WinRT;
//ImageSources
[assembly: ExportImageSourceHandler(typeof(FileImageSource), typeof(FileImageSourceHandler))]
-[assembly: ExportImageSourceHandler(typeof(StreamImageSource), typeof(StreamImagesourceHandler))]
-[assembly: ExportImageSourceHandler(typeof(UriImageSource), typeof(ImageLoaderSourceHandler))]
+[assembly: ExportImageSourceHandler(typeof(StreamImageSource), typeof(StreamImageSourceHandler))]
+[assembly: ExportImageSourceHandler(typeof(UriImageSource), typeof(UriImageSourceHandler))]
// Pages
diff --git a/Xamarin.Forms.Platform.WinRT/StreamImagesourceHandler.cs b/Xamarin.Forms.Platform.WinRT/StreamImagesourceHandler.cs
index 20d2c504..da2b1ff0 100644
--- a/Xamarin.Forms.Platform.WinRT/StreamImagesourceHandler.cs
+++ b/Xamarin.Forms.Platform.WinRT/StreamImagesourceHandler.cs
@@ -12,7 +12,7 @@ namespace Xamarin.Forms.Platform.UWP
namespace Xamarin.Forms.Platform.WinRT
#endif
{
- public sealed class StreamImagesourceHandler : IImageSourceHandler
+ public sealed class StreamImageSourceHandler : IImageSourceHandler
{
public async Task<Windows.UI.Xaml.Media.ImageSource> LoadImageAsync(ImageSource imagesource, CancellationToken cancellationToken = new CancellationToken())
{
diff --git a/Xamarin.Forms.Platform.WinRT/ImageLoaderSourceHandler.cs b/Xamarin.Forms.Platform.WinRT/UriImageSourceHandler.cs
index d2b223b5..1263c355 100644
--- a/Xamarin.Forms.Platform.WinRT/ImageLoaderSourceHandler.cs
+++ b/Xamarin.Forms.Platform.WinRT/UriImageSourceHandler.cs
@@ -14,11 +14,11 @@ namespace Xamarin.Forms.Platform.UWP
namespace Xamarin.Forms.Platform.WinRT
#endif
{
- public sealed class ImageLoaderSourceHandler : IImageSourceHandler
+ public sealed class UriImageSourceHandler : IImageSourceHandler
{
- public async Task<Windows.UI.Xaml.Media.ImageSource> LoadImageAsync(ImageSource imagesoure, CancellationToken cancellationToken = new CancellationToken())
+ public async Task<Windows.UI.Xaml.Media.ImageSource> LoadImageAsync(ImageSource imagesource, CancellationToken cancellationToken = new CancellationToken())
{
- var imageLoader = imagesoure as UriImageSource;
+ var imageLoader = imagesource as UriImageSource;
if (imageLoader?.Uri == null)
return null;
@@ -36,11 +36,10 @@ namespace Xamarin.Forms.Platform.WinRT
await image.SetSourceAsync(stream);
return image;
}
- catch (Exception ex)
+ catch (Exception ex)
{
- Debug.WriteLine(ex);
+ Log.Warning("Image Loading", $"{nameof(UriImageSourceHandler)} could not load {imageLoader.Uri}: {ex}");
- // Because this literally throws System.Exception
// According to https://msdn.microsoft.com/library/windows/apps/jj191522
// this can happen if the image data is bad or the app is close to its
// memory limit
diff --git a/Xamarin.Forms.Platform.WinRT/WindowsBasePlatformServices.cs b/Xamarin.Forms.Platform.WinRT/WindowsBasePlatformServices.cs
index 603ec401..49589a8a 100644
--- a/Xamarin.Forms.Platform.WinRT/WindowsBasePlatformServices.cs
+++ b/Xamarin.Forms.Platform.WinRT/WindowsBasePlatformServices.cs
@@ -99,7 +99,14 @@ namespace Xamarin.Forms.Platform.WinRT
using (var client = new HttpClient())
{
HttpResponseMessage streamResponse = await client.GetAsync(uri.AbsoluteUri).ConfigureAwait(false);
- return streamResponse.IsSuccessStatusCode ? await streamResponse.Content.ReadAsStreamAsync().ConfigureAwait(false) : null;
+
+ if (!streamResponse.IsSuccessStatusCode)
+ {
+ Log.Warning("HTTP Request", $"Could not retrieve {uri}, status code {streamResponse.StatusCode}");
+ return null;
+ }
+
+ return await streamResponse.Content.ReadAsStreamAsync().ConfigureAwait(false);
}
}
diff --git a/Xamarin.Forms.Platform.WinRT/Xamarin.Forms.Platform.WinRT.csproj b/Xamarin.Forms.Platform.WinRT/Xamarin.Forms.Platform.WinRT.csproj
index 6d28b900..f6b3566a 100644
--- a/Xamarin.Forms.Platform.WinRT/Xamarin.Forms.Platform.WinRT.csproj
+++ b/Xamarin.Forms.Platform.WinRT/Xamarin.Forms.Platform.WinRT.csproj
@@ -125,7 +125,7 @@
<Compile Include="HeightConverter.cs" />
<Compile Include="ICellRenderer.cs" />
<Compile Include="IImageSourceHandler.cs" />
- <Compile Include="ImageLoaderSourceHandler.cs" />
+ <Compile Include="UriImageSourceHandler.cs" />
<Compile Include="ImageRenderer.cs" />
<Compile Include="IVisualElementRenderer.cs" />
<Compile Include="LabelRenderer.cs" />