簡體   English   中英

永遠不會調用GetViewForAnnotation

[英]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只是弱訂閱了ObservableCollectionINotifyCollectionChanged事件,該事件在綁定中用作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不被命中?

我這樣做的方式是:

  • 具有包含MKMapView的ViewController的情節提要
  • 在IB的Connections Inspector中設置了MKMapView委托給ViewController

確保在ViewDidLoad進行設置: mapView.GetViewForAnnotation += GetViewForAnnotation;

我遇到了從來沒有被蜂鳴的問題,並且在添加一些注釋之前,請確保已為GetViewForAnnotation設置了事件,從而解決了該問題。

好的,顯然問題出在PEBKAC。

注釋確實已添加到MKMapView 但是,它們都沒有設置其Coordinate屬性。 因此,顯然地圖不知道在哪里展示它們,並且從未調用GetViewForAnnotation

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM