2016-03-22 23:02:25 +03:00
|
|
|
using System;
|
2016-08-02 23:54:57 +03:00
|
|
|
using System.Collections.Generic;
|
2017-04-11 21:02:06 +03:00
|
|
|
using System.ComponentModel;
|
2016-08-02 23:54:57 +03:00
|
|
|
using System.Reflection;
|
2016-05-03 01:19:12 +03:00
|
|
|
using Xamarin.Forms.Internals;
|
2016-03-22 23:02:25 +03:00
|
|
|
using Xamarin.Forms.Platform;
|
|
|
|
|
|
|
|
namespace Xamarin.Forms
|
|
|
|
{
|
|
|
|
[RenderWith(typeof(_ImageRenderer))]
|
2016-08-30 20:46:14 +03:00
|
|
|
public class Image : View, IImageController, IElementConfiguration<Image>
|
2016-03-22 23:02:25 +03:00
|
|
|
{
|
2016-08-02 23:54:57 +03:00
|
|
|
public static readonly BindableProperty SourceProperty = BindableProperty.Create("Source", typeof(ImageSource), typeof(Image), default(ImageSource),
|
|
|
|
propertyChanging: OnSourcePropertyChanging, propertyChanged: OnSourcePropertyChanged);
|
2016-03-22 23:02:25 +03:00
|
|
|
|
|
|
|
public static readonly BindableProperty AspectProperty = BindableProperty.Create("Aspect", typeof(Aspect), typeof(Image), Aspect.AspectFit);
|
|
|
|
|
|
|
|
public static readonly BindableProperty IsOpaqueProperty = BindableProperty.Create("IsOpaque", typeof(bool), typeof(Image), false);
|
|
|
|
|
|
|
|
internal static readonly BindablePropertyKey IsLoadingPropertyKey = BindableProperty.CreateReadOnly("IsLoading", typeof(bool), typeof(Image), default(bool));
|
|
|
|
|
|
|
|
public static readonly BindableProperty IsLoadingProperty = IsLoadingPropertyKey.BindableProperty;
|
|
|
|
|
2016-08-30 20:46:14 +03:00
|
|
|
readonly Lazy<PlatformConfigurationRegistry<Image>> _platformConfigurationRegistry;
|
|
|
|
|
|
|
|
public Image()
|
|
|
|
{
|
|
|
|
_platformConfigurationRegistry = new Lazy<PlatformConfigurationRegistry<Image>>(() => new PlatformConfigurationRegistry<Image>(this));
|
|
|
|
}
|
|
|
|
|
2016-03-22 23:02:25 +03:00
|
|
|
public Aspect Aspect
|
|
|
|
{
|
|
|
|
get { return (Aspect)GetValue(AspectProperty); }
|
|
|
|
set { SetValue(AspectProperty, value); }
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool IsLoading
|
|
|
|
{
|
|
|
|
get { return (bool)GetValue(IsLoadingProperty); }
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool IsOpaque
|
|
|
|
{
|
|
|
|
get { return (bool)GetValue(IsOpaqueProperty); }
|
|
|
|
set { SetValue(IsOpaqueProperty, value); }
|
|
|
|
}
|
|
|
|
|
|
|
|
[TypeConverter(typeof(ImageSourceConverter))]
|
|
|
|
public ImageSource Source
|
|
|
|
{
|
|
|
|
get { return (ImageSource)GetValue(SourceProperty); }
|
|
|
|
set { SetValue(SourceProperty, value); }
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnBindingContextChanged()
|
|
|
|
{
|
|
|
|
if (Source != null)
|
|
|
|
SetInheritedBindingContext(Source, BindingContext);
|
|
|
|
|
|
|
|
base.OnBindingContextChanged();
|
|
|
|
}
|
|
|
|
|
2017-04-07 10:47:33 +03:00
|
|
|
[Obsolete("OnSizeRequest is obsolete as of version 2.2.0. Please use OnMeasure instead.")]
|
2016-03-22 23:02:25 +03:00
|
|
|
protected override SizeRequest OnSizeRequest(double widthConstraint, double heightConstraint)
|
|
|
|
{
|
|
|
|
SizeRequest desiredSize = base.OnSizeRequest(double.PositiveInfinity, double.PositiveInfinity);
|
|
|
|
|
|
|
|
double desiredAspect = desiredSize.Request.Width / desiredSize.Request.Height;
|
|
|
|
double constraintAspect = widthConstraint / heightConstraint;
|
|
|
|
|
|
|
|
double desiredWidth = desiredSize.Request.Width;
|
|
|
|
double desiredHeight = desiredSize.Request.Height;
|
|
|
|
|
|
|
|
if (desiredWidth == 0 || desiredHeight == 0)
|
|
|
|
return new SizeRequest(new Size(0, 0));
|
|
|
|
|
|
|
|
double width = desiredWidth;
|
|
|
|
double height = desiredHeight;
|
|
|
|
if (constraintAspect > desiredAspect)
|
|
|
|
{
|
|
|
|
// constraint area is proportionally wider than image
|
|
|
|
switch (Aspect)
|
|
|
|
{
|
|
|
|
case Aspect.AspectFit:
|
|
|
|
case Aspect.AspectFill:
|
|
|
|
height = Math.Min(desiredHeight, heightConstraint);
|
|
|
|
width = desiredWidth * (height / desiredHeight);
|
|
|
|
break;
|
|
|
|
case Aspect.Fill:
|
|
|
|
width = Math.Min(desiredWidth, widthConstraint);
|
|
|
|
height = desiredHeight * (width / desiredWidth);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (constraintAspect < desiredAspect)
|
|
|
|
{
|
|
|
|
// constraint area is proportionally taller than image
|
|
|
|
switch (Aspect)
|
|
|
|
{
|
|
|
|
case Aspect.AspectFit:
|
|
|
|
case Aspect.AspectFill:
|
|
|
|
width = Math.Min(desiredWidth, widthConstraint);
|
|
|
|
height = desiredHeight * (width / desiredWidth);
|
|
|
|
break;
|
|
|
|
case Aspect.Fill:
|
|
|
|
height = Math.Min(desiredHeight, heightConstraint);
|
|
|
|
width = desiredWidth * (height / desiredHeight);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// constraint area is same aspect as image
|
|
|
|
width = Math.Min(desiredWidth, widthConstraint);
|
|
|
|
height = desiredHeight * (width / desiredWidth);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new SizeRequest(new Size(width, height));
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnSourceChanged(object sender, EventArgs eventArgs)
|
|
|
|
{
|
|
|
|
OnPropertyChanged(SourceProperty.PropertyName);
|
2016-05-03 01:19:12 +03:00
|
|
|
InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
|
2016-03-22 23:02:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void OnSourcePropertyChanged(BindableObject bindable, object oldvalue, object newvalue)
|
|
|
|
{
|
|
|
|
((Image)bindable).OnSourcePropertyChanged((ImageSource)oldvalue, (ImageSource)newvalue);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnSourcePropertyChanged(ImageSource oldvalue, ImageSource newvalue)
|
|
|
|
{
|
|
|
|
if (newvalue != null)
|
|
|
|
{
|
|
|
|
newvalue.SourceChanged += OnSourceChanged;
|
|
|
|
SetInheritedBindingContext(newvalue, BindingContext);
|
|
|
|
}
|
2016-08-02 23:54:57 +03:00
|
|
|
|
2016-05-03 01:19:12 +03:00
|
|
|
InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
|
2016-03-22 23:02:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void OnSourcePropertyChanging(BindableObject bindable, object oldvalue, object newvalue)
|
|
|
|
{
|
|
|
|
((Image)bindable).OnSourcePropertyChanging((ImageSource)oldvalue, (ImageSource)newvalue);
|
|
|
|
}
|
|
|
|
|
|
|
|
async void OnSourcePropertyChanging(ImageSource oldvalue, ImageSource newvalue)
|
|
|
|
{
|
|
|
|
if (oldvalue == null)
|
|
|
|
return;
|
2016-08-02 23:54:57 +03:00
|
|
|
|
2016-03-22 23:02:25 +03:00
|
|
|
oldvalue.SourceChanged -= OnSourceChanged;
|
2016-07-19 03:09:20 +03:00
|
|
|
try
|
|
|
|
{
|
|
|
|
await oldvalue.Cancel();
|
|
|
|
}
|
|
|
|
catch(ObjectDisposedException)
|
|
|
|
{
|
|
|
|
// Workaround bugzilla 37792 https://bugzilla.xamarin.com/show_bug.cgi?id=37792
|
|
|
|
}
|
2016-03-22 23:02:25 +03:00
|
|
|
}
|
2016-05-02 23:56:03 +03:00
|
|
|
|
2017-04-11 21:02:06 +03:00
|
|
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
|
|
|
public void SetIsLoading(bool isLoading)
|
2016-05-02 23:56:03 +03:00
|
|
|
{
|
|
|
|
SetValue(IsLoadingPropertyKey, isLoading);
|
|
|
|
}
|
2016-08-30 20:46:14 +03:00
|
|
|
|
|
|
|
public IPlatformElementConfiguration<T, Image> On<T>() where T : IConfigPlatform
|
|
|
|
{
|
|
|
|
return _platformConfigurationRegistry.Value.On<T>();
|
|
|
|
}
|
2016-03-22 23:02:25 +03:00
|
|
|
}
|
|
|
|
}
|