Xamarin.Forms - How does work OnElementPropertyChanged()

Emixam23Emixam23 USMember ✭✭✭

Hi,

I'm coming to speak about a problem that I have about a CustomMap

The thing is, I have a List<Position> in order to create a polyline on my Xamarin.Forms.Map in my CustomMap control/object. There a piece of the code:

public class CustomMap : Map
{
    public static readonly BindableProperty PolylineCoordinatesProperty =
       BindableProperty.Create(nameof(PolylineCoordinates), typeof(List<GeoPosition>), typeof(CustomMap), new List<GeoPosition>(), BindingMode.TwoWay);

    public List<GeoPosition> PolylineCoordinates
    {
        get { return (List<GeoPosition>)GetValue(PolylineCoordinatesProperty); }
        set { SetValue(PolylineCoordinatesProperty, value); }
    }
}

And then a CustomRenderer for each plateform, I'll use the UWP for the example:

public class CustomMapRenderer : MapRenderer
{
    MapControl nativeMap;
    CustomMap formsMap;

    protected override void OnElementChanged(ElementChangedEventArgs<Map> e)
    {
        base.OnElementChanged(e);

        if (e.OldElement != null)
        {
            nativeMap = Control as MapControl;
        }

        if (e.NewElement != null)
        {
            formsMap = (CustomMap)e.NewElement;
            nativeMap = Control as MapControl;
            UpdatePolyLine();
        }
    }

    protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);
        if (this.Element == null || this.Control == null)
            return;

        if (e.PropertyName == CustomMap.RouteCoordinatesProperty.PropertyName)
        {
            UpdatePolyLine();
        }
    }

    private void UpdatePolyLine()
    {
        if (formsMap != null && formsMap.RouteCoordinates.Count > 0)
        {
            List<BasicGeoposition> coordinates = new List<BasicGeoposition>();

            foreach (var position in formsMap.RouteCoordinates)
            {
                coordinates.Add(new BasicGeoposition() { Latitude = position.Latitude, Longitude = position.Longitude });
            }

            Geopath path = new Geopath(coordinates);
            MapPolyline polyline = new MapPolyline();
            polyline.StrokeColor = Windows.UI.Color.FromArgb(128, 255, 0, 0);
            polyline.StrokeThickness = 5;
            polyline.Path = path;
            nativeMap.MapElements.Add(polyline);
        }
    }
}

So, if I understand well the method OnElementProeprtyChanged() from the renderer, if the PolylineCoordinatesProperty of the CustomControl in the PCL part gets updated, OnElementProeprtyCHanged() is called, doesn't it? Then the polyline of my map is updated too? no?

You can find the solution just here => MapPolylineProject

I know it does work, but to make it work, I have to make everything from the XAML page, I mean, using the static methods of the CustomMap and then, I set the Attributes of the CustomMap.

But what I want to do, it is to make everything automatic ! So, by the update of the polyline address point attribute, the line is created automaticaly.

If anyone can help me, this one is welcome :)

Thank for reading !

Best Answer

Answers

  • Emixam23Emixam23 USMember ✭✭✭

    Hi, no idea? :/

  • Bert.3812Bert.3812 USMember

    Up! Got same issue :)

  • JoeMankeJoeManke USMember ✭✭✭✭✭
    edited September 2016

    Apologies if I'm misunderstanding what your problem is, but this may come in handy for you later anyways.

    The NotifyPropertyChanged event will only be fired when you set PolylineCoordinates to a different list. If you want an event to fire when you change the contents of the list (add/remove), change the property type from a List<Position> to an ObservableCollection<Position> and register for its CollectionChanged event.

Sign In or Register to comment.