Created
October 6, 2019 07:43
-
-
Save ufukhawk/80d18b0db992fb919d5a372dbaebf95c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[assembly: ExportRendererAttribute(typeof(IconView), typeof(IconViewRenderer))] | |
namespace IconApp.iOS.Renderers | |
{ | |
public class IconViewRenderer : ViewRenderer<IconView, UIImageView> | |
{ | |
private bool _isDisposed; | |
protected override void Dispose(bool disposing) | |
{ | |
if (_isDisposed) | |
{ | |
return; | |
} | |
if (disposing && base.Control != null) | |
{ | |
UIImage image = base.Control.Image; | |
UIImage uIImage = image; | |
if (image != null) | |
{ | |
uIImage.Dispose(); | |
uIImage = null; | |
} | |
} | |
_isDisposed = true; | |
base.Dispose(disposing); | |
} | |
protected override void OnElementChanged(ElementChangedEventArgs<IconView> e) | |
{ | |
base.OnElementChanged(e); | |
if (e.OldElement == null) | |
{ | |
UIImageView uIImageView = new UIImageView(CGRect.Empty) | |
{ | |
ContentMode = UIViewContentMode.ScaleAspectFit, | |
ClipsToBounds = true | |
}; | |
SetNativeControl(uIImageView); | |
} | |
if (e.NewElement != null) | |
{ | |
SetImage(e.OldElement); | |
} | |
} | |
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) | |
{ | |
base.OnElementPropertyChanged(sender, e); | |
if (e.PropertyName == IconView.SourceProperty.PropertyName) | |
{ | |
SetImage(null); | |
} | |
else if (e.PropertyName == IconView.ForegroundProperty.PropertyName) | |
{ | |
SetImage(null); | |
} | |
} | |
private void SetImage(IconView previous = null) | |
{ | |
if (previous == null && !string.IsNullOrWhiteSpace(Element.Source)) | |
{ | |
var uiImage = new UIImage(Element.Source); | |
uiImage = uiImage.ImageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate); | |
Control.TintColor = Element.Foreground.ToUIColor(); | |
Control.Image = uiImage; | |
if (!_isDisposed) | |
{ | |
((IVisualElementController)Element).NativeSizeChanged(); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment