MvvmCross iOS:如何绑定MapView注释跳转到另一个视图?

当标注附件button被点击时,如何绑定MapView的注释以切换到不同的视图? 如何实现注释的CalloutAccessoryControlTapped方法?

或者什么是最好的办法呢?

这是我的代码:

[Register("MapView")] public class MapView : MvxViewController { public override void ViewDidLoad() { Title = "Map"; base.ViewDidLoad(); var mapView = new MKMapView(new RectangleF(0, 0, 320, UIScreen.MainScreen.Bounds.Height - 20 - 44)) { MapType = MKMapType.Standard, ZoomEnabled = true, ScrollEnabled = true, Delegate = new MapDelegate(), }; View.AddSubview(mapView); var center = new CLLocationCoordinate2D(ViewModel.CurrentLat, ViewModel.CurrentLong); var span = new MKCoordinateSpan(5.0, 5.0); var region = new MKCoordinateRegion(center, span); mapView.SetRegion(region, true); mapView.AddAnnotation(CreateNewAnnotation(ViewModel.CurrentLat, ViewModel.CurrentLong, "You are here")); var set = this.CreateBindingSet<MapView, MapViewModel>(); set.Bind(mapView).For(???).To(vm => vm.showDetailCommand); // how can I bind to the map annotation and switch to other view when user click it? set.Apply(); } protected class MapDelegate : MKMapViewDelegate { public override MKAnnotationView GetViewForAnnotation(MKMapView mapView, MonoTouch.Foundation.NSObject annotation) { var pinId = "locationPinId"; var pinView = (LocationAnnotationView)mapView.DequeueReusableAnnotation(pinId) ?? new LocationAnnotationView { Annotation = annotation, RestorationIdentifier = pinId, Image = UIImage.FromBundle("images/map_pointer_icon_small.png"), }; var buttonView = new UIButton(new RectangleF(0, 0, 27, 27)); buttonView.SetImage(UIImage.FromBundle("images/blue_arrow.png"), UIControlState.Normal); buttonView.Tag = 88888; pinView.RightCalloutAccessoryView = buttonView; return pinView; } public override void CalloutAccessoryControlTapped(MKMapView mapView, MKAnnotationView view, UIControl control) { // how can I bind this method, so when the user click on the annotation it can switch to other view? } } } 

你可以用很多方法做到这一点。

鉴于您已经使用了固定的非绑定(非更新)单个项目显示,那么最简单的方法可能是扩展Annotation,以便使用ICommand以及lat,lng和标签创build:

 CreateNewAnnotation( ViewModel.CurrentLat, ViewModel.CurrentLong, ViewModel.ShowDetailCommand, "You are here") 

一旦完成, ICommand

  • 可以很容易地从您的call-out或从您的buttonView的TouchUpInsideCalloutAccessoryControlTapped处理程序CalloutAccessoryControlTapped
  • 可以在ShowDetailCommand作为导航命令实现 – 请参阅http://mvvmcross.wordpress.com/中&#x7684; N = 5,以获得导航的示例。

如果你想要一个更dynamic的注释 – 真正的数据绑定,那么你需要开始寻找添加数据绑定到你的自定义Annotation类,到你的自定义AnnotationView,也许还到你的自定义MapViewDelegate – 这将是类似的到数据绑定添加到UIView的方式 – 请参阅MvxView.cs – 但可能是你的当前示例矫枉过正