/// </summary>
public class CustomImageButton : Image
{
- public static readonly BindableProperty BlendColorProperty = BindableProperty.Create("BlendColor", typeof(Color), typeof(CustomImageButton), Color.Default);
+ public static readonly BindableProperty PressedColorProperty = BindableProperty.Create("PressedColor", typeof(Color), typeof(CustomImageButton), Color.Default);
public event EventHandler Clicked;
/// <summary>
/// A color when the button is pressed.
/// </summary>
- public Color BlendColor
+ public Color PressedColor
{
- get { return (Color)GetValue(BlendColorProperty); }
- set { SetValue(BlendColorProperty, value); }
+ get { return (Color)GetValue(PressedColorProperty); }
+ set { SetValue(PressedColorProperty, value); }
}
/// <summary>
/// </summary>
public CustomImageButton()
{
- BlendColor = Color.FromRgba(255, 255, 255, 100);
+ PressedColor = Color.FromRgba(255, 255, 255, 100);
}
}
}
public CustomImageButtonRenderer()
{
- RegisterPropertyHandler(CustomImageButton.BlendColorProperty, UpdateBlendColor);
+ RegisterPropertyHandler(CustomImageButton.PressedColorProperty, UpdatePressedColor);
}
/// <summary>
return;
}
- Control.Color = BtnElement.BlendColor.ToNative();
+ Control.Color = BtnElement.PressedColor.ToNative();
isTouched = true;
}
- private void UpdateBlendColor(bool obj)
+ private void UpdatePressedColor(bool obj)
{
CustomImageButton BtnElement = Element as CustomImageButton;
- Control.Color = BtnElement.BlendColor.ToNative();
+ Control.Color = BtnElement.PressedColor.ToNative();
}
/// <summary>
void UpdateAspect()
{
- Control.Aspect = Element.Aspect;
+ Control.ApplyAspect(Element.Aspect);
}
void UpdateIsOpaque()
{
public static readonly BindableProperty BlendColorProperty = BindableProperty.Create("BlendColor", typeof(Color), typeof(FormsElement), Color.Default);
+ public static readonly BindableProperty FileProperty = BindableProperty.Create("File", typeof(string), typeof(FormsElement), default(string));
+
public static Color GetBlendColor(BindableObject element)
{
return (Color)element.GetValue(BlendColorProperty);
SetBlendColor(config.Element, color);
return config;
}
+
+ public static string GetFile(BindableObject element)
+ {
+ return (string)element.GetValue(FileProperty);
+ }
+
+ public static void SetFile(BindableObject element, string file)
+ {
+ element.SetValue(FileProperty, file);
+ }
+
+ public static string GetFile(this IPlatformElementConfiguration<Tizen, FormsElement> config)
+ {
+ return GetFile(config.Element);
+ }
+
+ public static IPlatformElementConfiguration<Tizen, FormsElement> SetFile(this IPlatformElementConfiguration<Tizen, FormsElement> config, string file)
+ {
+ SetFile(config.Element, file);
+ return config;
+ }
}
}
+using EImage = ElmSharp.Image;
+
namespace Xamarin.Forms.Platform.Tizen
{
- internal static class ImageExtensions
+ public static class ImageExtensions
{
- internal static bool IsNullOrEmpty(this ImageSource imageSource) =>
+ public static void ApplyAspect(this EImage image, Aspect aspect)
+ {
+ Aspect _aspect = aspect;
+
+ switch (_aspect)
+ {
+ case Aspect.AspectFit:
+ image.IsFixedAspect = true;
+ image.CanFillOutside = false;
+ break;
+ case Aspect.AspectFill:
+ image.IsFixedAspect = true;
+ image.CanFillOutside = true;
+ break;
+ case Aspect.Fill:
+ image.IsFixedAspect = false;
+ image.CanFillOutside = false;
+ break;
+ default:
+ Log.Warn("Invalid Aspect value: {0}", _aspect);
+ break;
+ }
+ }
+
+ public static bool IsNullOrEmpty(this ImageSource imageSource) =>
imageSource == null || imageSource.IsEmpty;
}
}
/// </summary>
public class Image : EImage, IMeasurable
{
- Aspect _aspect;
-
/// <summary>
/// Initializes a new instance of the <see cref="Xamarin.Forms.Platform.Tizen.Native.Image"/> class.
/// </summary>
/// <param name="parent">The parent EvasObject.</param>
public Image(EvasObject parent) : base(parent)
- {
- IsScaling = true;
- CanScaleUp = true;
- CanScaleDown = true;
-
- ApplyAspect(Aspect.AspectFit);
- }
-
- /// <summary>
- /// Gets or sets the image aspect ratio preserving option.
- /// </summary>
- /// <value>The aspect option.</value>
- public Aspect Aspect
- {
- get
- {
- return _aspect;
- }
-
- set
- {
- if (_aspect != value)
- {
- ApplyAspect(value);
- }
- }
+ {
}
/// <summary>
return isLoadComplate;
}
+ public bool LoadFromFile(string file)
+ {
+ if (!string.IsNullOrEmpty(file))
+ {
+ return Load(ResourcePath.GetPath(file));
+ }
+ return false;
+ }
+
/// <summary>
/// Implements the <see cref="Xamarin.Forms.Platform.Tizen.Native.IMeasurable"/> interface.
/// </summary>
public ESize Measure(int availableWidth, int availableHeight)
{
var imageSize = ObjectSize;
-
var size = new ESize()
{
Width = imageSize.Width,
return size;
}
-
- /// <summary>
- /// Sets the <c>IsFixedAspect</c> and <c>CanFillOutside</c> properties according to the given <paramref name="aspect"/>.
- /// </summary>
- /// <param name="aspect">The aspect setting to be applied to the image.</param>
- void ApplyAspect(Aspect aspect)
- {
- _aspect = aspect;
-
- switch (_aspect)
- {
- case Aspect.AspectFit:
- IsFixedAspect = true;
- CanFillOutside = false;
- break;
-
- case Aspect.AspectFill:
- IsFixedAspect = true;
- CanFillOutside = true;
- break;
-
- case Aspect.Fill:
- IsFixedAspect = false;
- CanFillOutside = false;
- break;
-
- default:
- Log.Warn("Invalid Aspect value: {0}", _aspect);
- break;
- }
- }
}
}
\ No newline at end of file
void UpdateAspect()
{
- _image.Aspect = Element.Aspect;
+ _image.ApplyAspect(Element.Aspect);
}
}
}
{
public class ImageRenderer : ViewRenderer<Image, Native.Image>
{
+ public ImageRenderer()
+ {
+ RegisterPropertyHandler(Image.SourceProperty, UpdateSource);
+ RegisterPropertyHandler(Image.AspectProperty, UpdateAspect);
+ RegisterPropertyHandler(Image.IsOpaqueProperty, UpdateIsOpaque);
+ RegisterPropertyHandler(Specific.BlendColorProperty, UpdateBlendColor);
+ RegisterPropertyHandler(Specific.FileProperty, UpdateFile);
+ }
+
protected override void OnElementChanged(ElementChangedEventArgs<Image> e)
{
if (Control == null)
{
- var image = NativeFactory.GetNativeControl(typeof(Native.Image)) as Native.Image;
- SetNativeControl(image);
+ SetNativeControl(new Native.Image(Forms.NativeParent));
}
-
- UpdateAll();
base.OnElementChanged(e);
}
- protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
+ async void UpdateSource(bool initialize)
{
- base.OnElementPropertyChanged(sender, e);
- if (e.PropertyName == Image.SourceProperty.PropertyName)
- {
- UpdateSource();
- }
- else if (e.PropertyName == Image.AspectProperty.PropertyName)
- {
- UpdateAspect();
- }
- else if (e.PropertyName == Image.IsOpaqueProperty.PropertyName)
- {
- UpdateIsOpaque();
- }
- else if (e.PropertyName == Specific.BlendColorProperty.PropertyName)
- {
- UpdateBlendColor();
- }
- }
+ if (initialize && Element.Source == default(ImageSource))
+ return;
- 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();
- UpdateAfterLoading();
+ UpdateAfterLoading(initialize);
}
}
((IImageController)Element).SetIsLoading(false);
}
- protected virtual void UpdateAfterLoading()
+ void UpdateFile(bool initialize)
{
- UpdateIsOpaque();
- UpdateBlendColor();
+ if (initialize && Specific.GetFile(Element) == default || Element.Source != default(ImageSource))
+ return;
+
+ if (Control != null)
+ {
+ bool success = Control.LoadFromFile(Specific.GetFile(Element));
+
+ if (!IsDisposed && success)
+ {
+ ((IVisualElementController)Element).NativeSizeChanged();
+ UpdateAfterLoading(initialize);
+ }
+ }
}
- void UpdateAspect()
+ protected virtual void UpdateAfterLoading(bool initialize)
{
- Control.Aspect = Element.Aspect;
+ UpdateIsOpaque(initialize);
+ UpdateBlendColor(initialize);
}
- void UpdateIsOpaque()
+ void UpdateAspect(bool initialize)
{
- Control.IsOpaque = Element.IsOpaque;
+ if (initialize && Element.Aspect == Aspect.AspectFit)
+ return;
+
+ Control.ApplyAspect(Element.Aspect);
}
- void UpdateBlendColor()
+ void UpdateIsOpaque(bool initialize)
{
- Control.Color = Specific.GetBlendColor(Element).ToNative();
+ if (initialize && !Element.IsOpaque)
+ return;
+
+ Control.IsOpaque = Element.IsOpaque;
}
- void UpdateAll()
+ void UpdateBlendColor(bool initialize)
{
- UpdateSource();
- UpdateAspect();
+ if (initialize && Specific.GetBlendColor(Element).IsDefault)
+ return;
+
+ Control.Color = Specific.GetBlendColor(Element).ToNative();
}
}