为什么我的事件没有在 Xamarin UWP 地图自定义呈现器中触发



我正在尝试为 UWP 上的 Xamarin 映射编写自定义呈现器,但 PCL 中的集合更改事件未触发 UWP 自定义呈现器中的相应事件。它在iOS和Android上运行良好。

使用以下代码,事件 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)
    {
       ;
    }
  }
 }

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

相关内容

  • 没有找到相关文章

最新更新