永远不会调用 GetViewForAnnotation
GetViewForAnnotation is never called
我正在创建一个 MapView,我想在其中显示一些自定义注释。
所以我认为通常您所做的是使用 AddAnnotation
方法将一些 IMKAnnotation
添加到 MKMapView
。我确保在主线程上调用它,例如:
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
简单地弱订阅 INotifyCollectionChanged
事件 ObservableCollection
在绑定中用作 ItemsSource
。当集合发生变化时,它只是在 MKMapView
中添加和删除注释,这里没有发生任何神奇的事情。我已经验证它确实在 MKMapView
中添加了不同的 IMKAnnotation
实例,在本例中为 13,并且可以在它的 Annotations
属性 中检查它们。
因此,正如@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
不会触发?
我的做法是:
- 故事板 ViewController 其中包含 MKMapView
- MKMapView Delegate to ViewController 在 IB 的 Connections Inspector 中设置
在 ViewDidLoad
中,我确保我首先设置了这个:
mapView.GetViewForAnnotation += GetViewForAnnotation;
我也遇到过它从未被调用过的问题,我通过确保在添加一些注释之前为 GetViewForAnnotation 设置事件来修复它。
好的,显然问题出在 PEBKAC 上。
注释确实添加到了 MKMapView
。但是,他们中的 none 有 Coordinate
属性 组。所以显然地图不知道在哪里显示它们并且从未调用 GetViewForAnnotation
。
我正在创建一个 MapView,我想在其中显示一些自定义注释。
所以我认为通常您所做的是使用 AddAnnotation
方法将一些 IMKAnnotation
添加到 MKMapView
。我确保在主线程上调用它,例如:
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
简单地弱订阅 INotifyCollectionChanged
事件 ObservableCollection
在绑定中用作 ItemsSource
。当集合发生变化时,它只是在 MKMapView
中添加和删除注释,这里没有发生任何神奇的事情。我已经验证它确实在 MKMapView
中添加了不同的 IMKAnnotation
实例,在本例中为 13,并且可以在它的 Annotations
属性 中检查它们。
因此,正如@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
不会触发?
我的做法是:
- 故事板 ViewController 其中包含 MKMapView
- MKMapView Delegate to ViewController 在 IB 的 Connections Inspector 中设置
在 ViewDidLoad
中,我确保我首先设置了这个:
mapView.GetViewForAnnotation += GetViewForAnnotation;
我也遇到过它从未被调用过的问题,我通过确保在添加一些注释之前为 GetViewForAnnotation 设置事件来修复它。
好的,显然问题出在 PEBKAC 上。
注释确实添加到了 MKMapView
。但是,他们中的 none 有 Coordinate
属性 组。所以显然地图不知道在哪里显示它们并且从未调用 GetViewForAnnotation
。