Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

[Bug] Polygon.Points doesn't respond to CollectionChanged events #11563

Closed
Inrego opened this issue Jul 26, 2020 · 1 comment
Closed

[Bug] Polygon.Points doesn't respond to CollectionChanged events #11563

Inrego opened this issue Jul 26, 2020 · 1 comment

Comments

@Inrego
Copy link

Inrego commented Jul 26, 2020

Description

When modifying the points collection of Polygon, the shape doesn't change, even though PointsCollection implements INotifyCollectionChanged.

Steps to Reproduce

  1. Make polygon
  2. Add tap gesture recognizer, which will add, replace or remove points from the polygon

Expected Behavior

The polygon will change to reflect the current collection of points.

Actual Behavior

Polygon doesn't change.

Basic Information

  • Version with issue: 4.7.0.1142
  • Platform Target Frameworks:
    Only tested on Android, I expect it's a general problem.

Screenshots

Reproduction Link

Workaround

Replace the whole Points collection with a new collection with the modified points. It's quite ineffective for animations for example.

@Inrego Inrego added s/unverified New report that has yet to be verified t/bug 🐛 labels Jul 26, 2020
@Inrego
Copy link
Author

Inrego commented Jul 26, 2020

I managed a better workaround. Custom renderers:
iOS

public class CustomPolygonRenderer : PolygonRenderer
{
    private PointCollection _points;
    protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs args)
    {
        base.OnElementPropertyChanged(sender, args);
        if (args.PropertyName == Polygon.PointsProperty.PropertyName)
        {
            var polygon = (Polygon)sender;
            if (_points != null)
                _points.CollectionChanged -= PointsOnCollectionChanged;
            _points = polygon.Points;
            _points.CollectionChanged += PointsOnCollectionChanged;
        }
    }

    protected override void OnElementChanged(ElementChangedEventArgs<Polygon> args)
    {
        base.OnElementChanged(args);
        if (args.NewElement == null && _points != null)
        {
            _points.CollectionChanged -= PointsOnCollectionChanged;
        }
    }

    private void PointsOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        Control.UpdatePoints(_points.ToCGPoints());
    }
}

Android

public class CustomPolygonRenderer : PolygonRenderer
{
    public CustomPolygonRenderer(Context context) : base(context)
    {
    }

    private PointCollection _points;
    protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs args)
    {
        base.OnElementPropertyChanged(sender, args);
        if (args.PropertyName == Polygon.PointsProperty.PropertyName)
        {
            var polygon = (Polygon) sender;
            if (_points != null)
                _points.CollectionChanged -= PointsOnCollectionChanged;
            _points = polygon.Points;
            _points.CollectionChanged += PointsOnCollectionChanged;
        }
    }

    protected override void OnElementChanged(ElementChangedEventArgs<Polygon> args)
    {
        base.OnElementChanged(args);
        if (args.NewElement == null && _points != null)
        {
            _points.CollectionChanged -= PointsOnCollectionChanged;
        }
    }


    private void PointsOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        Control.UpdatePoints(Element.Points);
    }
}

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

No branches or pull requests

2 participants