MvvmCross iOS:如何绑定MapView注释以跳转到另一个视图? [英] MvvmCross iOS: How to bind MapView Annotation to jump to another view?

查看:80
本文介绍了MvvmCross iOS:如何绑定MapView注释以跳转到另一个视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单击标注附件按钮时,如何绑定MapView的注释以切换到其他视图?如何实现注释的CalloutAccessoryControlTapped方法?

How can I bind the MapView's annotation to switch to different view when it's callout accessory button is clicked? How do I implement the annotation's CalloutAccessoryControlTapped method?

或者最好的方法是什么?

Or what is the best way to do it?

这是我的代码:

[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?
        }
    }
}

推荐答案

您可以通过多种方式进行此操作.

You could do this in lots of ways.

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

Given that you are already using fixed non-binding (non-updating) single item display, then the easiest approach is probably for you to just extend your Annotation so that it is created with an ICommand as well as the lat, lng and label:

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

完成此操作后,然后ICommand:

    然后,可以轻松地从您的标注或buttonView的TouchUpInsideCalloutAccessoryControlTapped处理程序中调用
  • .
  • 可以作为导航命令在ShowDetailCommand内部实现-参见 http://mvvmcross.wordpress中的N = 5. com/以获取导航示例.
  • can then easily be invoked from your call-out or from your buttonView's TouchUpInside or from your CalloutAccessoryControlTapped handler.
  • can be implemented inside ShowDetailCommand as a navigation command - see N=5 in http://mvvmcross.wordpress.com/ for examples of navigations.

如果您确实想要更多的动态注释-具有真正的数据绑定,那么您将需要开始考虑将数据绑定添加到自定义Annotation类,自定义AnnotationView以及自定义MapViewDelegate中-类似于将数据绑定添加到UIView的方式-请参见

If you do want a much more dynamic annotation - with true data-binding, then you would need to start looking at adding data-binding to your custom Annotation class, to your custom AnnotationView and perhaps also to your custom MapViewDelegate - this would be similar to the way data-binding is added to a UIView - see MvxView.cs - but is probably overkill for your current example.

这篇关于MvvmCross iOS:如何绑定MapView注释以跳转到另一个视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆