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

时间:2013-07-19 23:09:15

标签: ios xamarin mvvmcross

如果点击了标注附件按钮,如何绑定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?
        }
    }
}

1 个答案:

答案 0 :(得分:1)

你可以通过多种方式实现这一目标。

鉴于您已经在使用固定的非绑定(非更新)单项显示,那么最简单的方法可能是您只需扩展Annotation,以便使用ICommand创建它以及lat,lng和label:

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

完成后,ICommand

    然后可以通过您的标注或buttonView的TouchUpInsideCalloutAccessoryControlTapped处理程序轻松调用
  • 可以在ShowDetailCommand内作为导航命令实现 - 有关导航的示例,请参阅http://mvvmcross.wordpress.com/中的N = 5。

如果你想要一个更加动态的注释 - 使用真正的数据绑定,那么你需要开始考虑将数据绑定添加到自定义Annotation类,自定义AnnotationView以及可能还有自定义MapViewDelegate - 这个类似于将数据绑定添加到UIView的方式 - 请参阅MvxView.cs - 但对于您当前的示例可能 overkill