maui-linux/Xamarin.Forms.Platform.UAP/UriImageSourceHandler.cs

45 строки
1.2 KiB
C#
Исходник Обычный вид История

2016-03-22 23:02:25 +03:00
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Windows.Storage.Streams;
using Windows.UI.Xaml.Media.Imaging;
using Xamarin.Forms.Internals;
2016-03-22 23:02:25 +03:00
namespace Xamarin.Forms.Platform.UWP
{
public sealed class UriImageSourceHandler : IImageSourceHandler
2016-03-22 23:02:25 +03:00
{
public async Task<Windows.UI.Xaml.Media.ImageSource> LoadImageAsync(ImageSource imagesource, CancellationToken cancellationToken = new CancellationToken())
2016-03-22 23:02:25 +03:00
{
var imageLoader = imagesource as UriImageSource;
2016-03-22 23:02:25 +03:00
if (imageLoader?.Uri == null)
return null;
Stream streamImage = await imageLoader.GetStreamAsync(cancellationToken);
if (streamImage == null || !streamImage.CanRead)
{
return null;
}
2016-03-25 23:53:19 +03:00
using (IRandomAccessStream stream = streamImage.AsRandomAccessStream())
2016-03-22 23:02:25 +03:00
{
try
{
var image = new BitmapImage();
await image.SetSourceAsync(stream);
return image;
}
catch (Exception ex)
2016-03-22 23:02:25 +03:00
{
Log.Warning("Image Loading", $"{nameof(UriImageSourceHandler)} could not load {imageLoader.Uri}: {ex}");
2016-03-22 23:02:25 +03:00
// 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
return null;
}
}
}
}
}