2016-05-20 2 views
0

Я пытаюсь написать собственный рендерер для карты Xamarin в UWP, а событие изменения коллекции в PCL не вызывает соответствующее событие в пользовательском рендерере UWP. Он отлично работает на iOS и Android.Почему мое событие не стреляет в Xamarin UWP Map CustomRenderer?

В следующем коде событие ItemsCollectionChanged никогда не вызывается в CustomMapRenderer, даже через OnItemsSourcePropertyChanged вызывается каждые 5 секунд.

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) 
    { 
     ; 
    } 
    } 
} 

ответ

0

Я использовал синглтон, чтобы получить наблюдаемую коллекцию, которая мне нужна

Смежные вопросы