[英]GetViewForAnnotation is never called
我正在创建一个MapView,我想在其中显示一些自定义注释。
因此,我认为通常您使用AddAnnotation
方法向MKMapView
添加一些IMKAnnotation
。 我确保在主线程上调用它,例如:
new NSObject().InvokeOnMainThread(() => {
_mapView.AddAnnotation(myNewAnnotation);
});
添加完这些之后,我确实看到MKMapView
现在包含在使用调试器进行检查时在Annotations
属性中添加的所有注释。
但是,问题是无论我如何执行,都不会调用GetViewForAnnotation
。
我试过了:
_mapView.GetViewForAnnotation += ViewForAnnotation;
private MKAnnotationView ViewForAnnotation(MKMapView mapView, IMKAnnotation annotation) {
// do stuff here
}
我尝试实现自己的委托:
public class MyMapViewDelegate : MKMapViewDelegate
{
public override MKAnnotationView GetViewForAnnotation(MKMapView mapView, IMKAnnotation annotation) {
// do stuff
}
}
_delegate = new MyMapViewDelegate();
_mapView.Delegate = _delegate;
我试过使用WeakDelegate
:
public class MapView : ViewController, IMKMapViewDelegate
{
private MKMapView _mapView;
public override void ViewDidLoad() {
_mapView = new MKMapView();
_mapView.WeakDelegate = this;
}
[Export("mapView:viewForAnnotation:")]
public MKAnnotationView GetViewForAnnotation(MKMapView mapView, IMKAnnotation annotation) {
// do stuff
}
}
似乎没有什么触发GetViewForAnnotation
方法。 有什么想法我做错了吗?
编辑:
我现在有更多的细节。
[Register("MapView")]
public class MapView : MvxViewController<MapViewModel>
{
private MKMapView _mapView;
private NMTAnnotationManager _annotationManager;
public override void ViewDidLoad()
{
base.ViewDidLoad();
_mapView = new MKMapView();
_mapView.GetViewForAnnotation += GetViewForAnnotation;
_annotationManager = new NMTAnnotationManager(_mapView);
var bindSet = this.CreateBindingSet<MapView, MapViewModel>();
bindSet.Bind(_annotationManager).For(a => a.ItemsSource).To(vm => vm.Locations).OneWay();
bindSet.Apply();
Add(_mapView);
View.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();
View.AddConstraints(
_mapView.AtTopOf(View),
_mapView.AtLeftOf(View),
_mapView.AtRightOf(View),
_mapView.AtBottomOf(View));
}
private MKAnnotationView GetViewForAnnotation(MKMapView mapview, IMKAnnotation annotation)
{
return null;
}
}
NMTAnnotationManager
只是弱订阅了ObservableCollection
的INotifyCollectionChanged
事件,该事件在绑定中用作ItemsSource
。 当集合发生更改时,它只需在MKMapView
添加和删除Annotations,这里就不会发生任何神奇的事情。 我已经验证了它确实在这种情况下向MKMapView
添加了不同的IMKAnnotation
实例,并且可以在其Annotations
属性中对其进行检查,它们确实是13个。
因此,作为@Philip在他的回答表明, GetViewForAnnotation
注释添加到的MapView之前不会置位。 但是,如果我在方法中放置一个断点或一些痕迹,它将永远不会受到打击。
上面的相同代码,只是带有一个简单的MKMapViewDelegate
如下所示:
public class MyMapViewDelegate : MKMapViewDelegate
{
public override void MapLoaded(MKMapView mapView)
{
Mvx.Trace("MapLoaded");
}
public override MKAnnotationView GetViewForAnnotation(MKMapView mapView, IMKAnnotation annotation)
{
Mvx.Trace("GetViewForAnnotation");
return null;
}
}
也不起作用。 虽然,每次渲染地图时MapLoaded
事件都会被命中,但是为什么GetViewForAnnotation
不被命中?
我这样做的方式是:
确保在ViewDidLoad
进行设置: mapView.GetViewForAnnotation += GetViewForAnnotation;
我遇到了从来没有被蜂鸣的问题,并且在添加一些注释之前,请确保已为GetViewForAnnotation设置了事件,从而解决了该问题。
好的,显然问题出在PEBKAC。
注释确实已添加到MKMapView
。 但是,它们都没有设置其Coordinate
属性。 因此,显然地图不知道在哪里展示它们,并且从未调用GetViewForAnnotation
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.