0

I am trying to write a custom renderer for a Xamarin Map on UWP and a collection change event in the PCL is not triggering the appropriate event in the UWP custom renderer. It works just fine on iOS and Android.

With the following code the event ItemsCollectionChanged never gets called in the CustomMapRenderer even through the OnItemsSourcePropertyChanged is being called every 5 seconds.

   public class CustomMap : Map
   { 

    #region << Events >>

    public event EventHandler ItemsCollectionChanged;

    #endregion

          private static void OnItemsSourcePropertyChanged(BindableObject bindable, object oldValue, object newValue)
          {
                 var map = bindable as CustomMap;

                 if (map == null)
                       return;

                 map.ItemsSource.CollectionChanged += (s, e) =>
                 {
                       SetPin(bindable);
                       if (map.ItemsCollectionChanged != null)
                       {
                              map.ItemsCollectionChanged(bindable, new EventArgs());
                       }
                 }; 
          }

   }

  [assembly: ExportRenderer(typeof(CustomMap), typeof(CustomMapRenderer))]
 Namespace MyNamespace.Renderers
{
  public class CustomMapRenderer : MapRenderer
 {
    MapControl _nativeMap;


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

        if (e.OldElement != null)
        {
            _nativeMap.MapElementClick -= OnMapElementClick;
            _nativeMap.Children.Clear();
            _nativeMap = null;
        }

        if (e.NewElement != null)
        {
            var formsMap = (CustomMap)e.NewElement;
            formsMap.ItemsCollectionChanged += ItemsCollectionChanged;

            _pinClickedCommand = formsMap.PinClickedCommand;
            _routeCoordinates = formsMap.ItemsSource;
            _nativeMap = Control as MapControl;
            _nativeMap.Children.Clear();
            _nativeMap.MapElementClick += OnMapElementClick;
            var snPosition = new BasicGeoposition { Latitude = 45, Longitude = -88 };
            Geopoint snPoint = new Geopoint(snPosition);

            var mapIcon = new MapIcon();
            if (mapIcon != null)
            {
                _nativeMap.MapElements.Remove(mapIcon);
            }

            mapIcon.CollisionBehaviorDesired = MapElementCollisionBehavior.RemainVisible;
            mapIcon.Location = snPoint;
            mapIcon.NormalizedAnchorPoint = new Windows.Foundation.Point(0.5, 1.0);
            _nativeMap.MapElements.Add(mapIcon);
            _nativeMap.Center = snPoint;
        }

    }


    void ItemsCollectionChanged(object sender, EventArgs e)
    {
       ;
    }
  }
 }
rbrundritt
  • 16,570
  • 2
  • 21
  • 46
ArbiterUnknown
  • 113
  • 2
  • 13

1 Answers1

0

I used a singleton to get the observable collection that I needed

ArbiterUnknown
  • 113
  • 2
  • 13