0

我正在尝试为 UWP 上的 Xamarin Map 编写自定义渲染器,并且 PCL 中的集合更改事件未触发 UWP 自定义渲染器中的相应事件。它在 iOS 和 Android 上运行良好。

使用以下代码,即使每 5 秒调用一次 OnItemsSourcePropertyChanged,也不会在 CustomMapRenderer 中调用事件 ItemsCollectionChanged。

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

1 回答 1

0

我使用单例来获取我需要的可观察集合

于 2016-05-20T19:11:38.730 回答