Skip to content
This repository was archived by the owner on Apr 3, 2023. It is now read-only.

Master#90

Open
fabricetayou wants to merge 2 commits intojamesmontemagno:masterfrom
fabricetayou:master
Open

Master#90
fabricetayou wants to merge 2 commits intojamesmontemagno:masterfrom
fabricetayou:master

Conversation

@fabricetayou
Copy link

I used the code of @VladislavAntonyuk, it worked and I applied here

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest you to use PackageReference

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bee8328 is ok

return;

file = Element.Source;

Copy link

@VladislavAntonyuk VladislavAntonyuk Feb 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IImageSourceHandler handler;

            switch (file)
            {
                case UriImageSource _:
                    handler = new UriImageSourceHandler();
                    break;
                case FileImageSource f:
                    if(!File.Exists(f.File))
                    {
                        return;
                    }

                    handler = new FileImageSourceHandler();
                    break;
                case StreamImageSource _:
                    handler = new StreamImageSourceHandler();
                    break;
                default:
                    return;
            }

return;
}

if (bitmapImage != null)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var originalBitmap = await handler.LoadImageAsync(Element.Source);
            if (originalBitmap != null)
            {
                Control.Fill = new ImageBrush
                {
                    ImageSource = originalBitmap,
                    Stretch = Stretch.UniformToFill
                };
            }

/// <summary>
/// ImageCircle Implementation
/// </summary>
public class ImageCircleRenderer : ViewRenderer<Image, Ellipse>
Copy link

@VladislavAntonyuk VladislavAntonyuk Feb 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have changed a code for this file:

private ImageSource file;

        protected override void OnElementChanged(ElementChangedEventArgs<Image> e)
        {
            base.OnElementChanged(e);
            if (e.OldElement == null)
            {
                var ellipse = new Ellipse();
                SetNativeControl(ellipse);
            }
        }

        protected override async void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            if (Control == null)
            {
                return;
            }

            var min = Math.Min(Element.Width, Element.Height);
            if (min / 2.0f <= 0)
            {
                return;
            }

            Control.Width = min;
            Control.Height = min;

            // Fill background color
            var color = ((CircleImage) Element).FillColor;
            Control.Fill = XamarinColorToBrush(color);

            // Fill stroke
            color = ((CircleImage) Element).BorderColor;
            Control.StrokeThickness = ((CircleImage) Element).BorderThickness;
            Control.Stroke = XamarinColorToBrush(color);

            var force = ForceUpdate(e.PropertyName);

            //already set
            if (file == Element.Source && !force)
            {
                return;
            }

            file = Element.Source;

            IImageSourceHandler handler;

            switch (file)
            {
                case UriImageSource _:
                    handler = new UriImageSourceHandler();
                    break;
                case FileImageSource f:
                    if(!File.Exists(f.File))
                    {
                        return;
                    }

                    handler = new FileImageSourceHandler();
                    break;
                case StreamImageSource _:
                    handler = new StreamImageSourceHandler();
                    break;
                default:
                    return;
            }

            var originalBitmap = await handler.LoadImageAsync(Element.Source);
            if (originalBitmap != null)
            {
                Control.Fill = new ImageBrush
                {
                    ImageSource = originalBitmap,
                    Stretch = Stretch.UniformToFill
                };
            }
        }

        private static bool ForceUpdate(string propertyName)
        {
            var forcePropertiesList = new List<string>
            {
                VisualElement.XProperty.PropertyName,
                VisualElement.YProperty.PropertyName,
                VisualElement.WidthProperty.PropertyName,
                VisualElement.HeightProperty.PropertyName,
                VisualElement.ScaleProperty.PropertyName,
                VisualElement.TranslationXProperty.PropertyName,
                VisualElement.TranslationYProperty.PropertyName,
                VisualElement.RotationYProperty.PropertyName,
                VisualElement.RotationXProperty.PropertyName,
                VisualElement.RotationProperty.PropertyName,
                CircleImage.BorderThicknessProperty.PropertyName,
                CircleImage.BorderColorProperty.PropertyName,
                CircleImage.FillColorProperty.PropertyName,
                VisualElement.AnchorXProperty.PropertyName,
                VisualElement.AnchorYProperty.PropertyName
            };

            return forcePropertiesList.Contains(propertyName);
        }

        private static Brush XamarinColorToBrush(Color color)
        {
            return new SolidColorBrush(System.Windows.Media.Color.FromArgb(
                (byte) (color.A * 255),
                (byte) (color.R * 255),
                (byte) (color.G * 255),
                (byte) (color.B * 255)));
        }

/// Used for registration with dependency service
/// </summary>
public async static void Init()
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Init is not necessary, I suggest to replace the file with the code above

Stretch = Stretch.UniformToFill
};
}
catch

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try catch looks uninformative, you can remove it

Copy link

@VladislavAntonyuk VladislavAntonyuk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the pull request. I would appreciate if you replace the CircleImageRenderer with a new code,
also please add WPF to the readme.md

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants